Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

javascript - Does Node.js enforce a minimum delay for setTimeout?

In browsers, if you use setTimeout from within a function called by setTimeout then a minimum delay of 4ms will be enforced. Mozilla's developer wiki describes this behaviour, and mentions that it has become standardized in HTML5.

Node.js's documentation for setTimeout does not mention a minimum delay. However, the documentation for the process.nextTick function describes it as a more efficient alternative to setTimeout(fn, 0). This suggests the possibility that it's more efficient because it's avoiding this delay. Otherwise setTimeout(fn, 0) could probably be optimized to behave the same.

Does Node.js enforce a minimum delay for setTimeout, as web browsers do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It doesn't have a minimum delay and this is actually a compatibility issue between browsers and node. Timers are completely unspecified in JavaScript (it's a DOM specification which has no use in Node and isn't even followed by browsers anyway) and node implements them simply due to how fundamental they've been in JavaScript's history and how irreplaceable they are otherwise.

Node uses libuv which a cross-platform abstraction layer for lower level system things like file-system, networking stuff, etc. One of those things is timers, which Node provides a minimal wrapper around. At the libuv level, the timers used are the system-specific high precision timers. In Windows, for example, this is implemented using QueryPerformanceFrequency and FileTimeToSystemTime which provides resolution measured in nanoseconds.

In Node, if you specify setTimeout(callback, 1) then it will be executed one millisecond later (assuming the system doesn't delay it due to be overwhelmed). In browsers, the minimum time will be 4 milliseconds as specified by the HTML5 spec: https://developer.mozilla.org/en/DOM/window.setTimeout. This isn't a guaranteed time, just a minimum. Most browsers can be expected to have a ~15ms resolution which impacts DOM animations.

One valid piece of info is that timeouts set to the same millisecond, during the same frame, will be executed in the order they were queued. If you were to do:

  setTimeout(callback1, 1);
  setTimeout(callback2, 1);
  setTimeout(callback3, 1);
  setTimeout(callback4, 1);

All in one block, Node should call them in that order. This only applies if they have the exact same resolution time.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644905(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724280(v=vs.85).aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...