-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Document what setTimeout(callback, 0) does #7447
Conversation
Thank you for contributing this pull request! Here are a few pointers to make sure your submission will be considered for inclusion. Commit dandv/node@352191d has the following error(s):
The following commiters were not found in the CLA:
You can fix all these things without opening another issue. Please see CONTRIBUTING.md for more information |
Suggestion: recommend |
Actually, don't recommend |
We should be more specific here. Recursive setImmediate doesn't starve |
Is it necessary to document |
+1 on using "event loop turn". -1 on making |
Good to see a discussion started. I'm not a core node dev; just a user who has run into unexpected and undocumented behavior for |
this has split into 2 discussions
edit: added the word 'helped' |
In a browser using standard Is it the same in Node.js? |
var totalNanoseconds = 0;
var count = 1e3;
var NS_TO_S = 1e-9;
var S_TO_NS = 1e9;
var S_TO_MS = 1e3;
var AVERAGE = NS_TO_S * S_TO_MS / count;
function noTimeout(cb) {
var start = process.hrtime();
setTimeout(function () {
var finish = process.hrtime(start);
cb(finish[0] * S_TO_NS + finish[1]);
}, 0);
}
function run() {
noTimeout(function (time) {
totalNanoseconds += time;
if (--count > 0) return run();
console.log("Loop took an average of %sms per iteration.", totalNanoseconds * AVERAGE);
});
}
run(); produces
If you look at the code, you'll see that you're probably never going to get it to run in much less than a millisecond because of the built-in floor to the duration, which, as the comment helpfully notes, is there to provide compatibility with browsers. Try replacing the call to |
@othiym23 @trevnorris ... recognizing that we likely still need to do a better job explaining nextTick/setImmediate in the docs, is there reason to keep this PR open? |
Related: #6725 |
New issue opened in nodejs/docs. Closing this here but still need to revisit. |
CLA signed. I've edited via the GitHub UI - sorry for forgetting to add the "Docs" subsystem in the commit message.