diff --git a/benchmark/timers/timers-timeout-nexttick.js b/benchmark/timers/timers-timeout-nexttick.js new file mode 100644 index 00000000000000..781e505ae41ea2 --- /dev/null +++ b/benchmark/timers/timers-timeout-nexttick.js @@ -0,0 +1,36 @@ +'use strict'; +const common = require('../common.js'); + +// The following benchmark measures setting up n * 1e6 timeouts, +// as well as scheduling a next tick from each timeout. Those +// then get executed on the next uv tick. + +const bench = common.createBenchmark(main, { + n: [5e4, 5e6], +}); + +function main({ n }) { + let count = 0; + + // Function tracking on the hidden class in V8 can cause misleading + // results in this benchmark if only a single function is used — + // alternate between two functions for a fairer benchmark. + + function cb() { + process.nextTick(counter); + } + function cb2() { + process.nextTick(counter); + } + function counter() { + count++; + if (count === n) + bench.end(n); + } + + for (var i = 0; i < n; i++) { + setTimeout(i % 2 ? cb : cb2, 1); + } + + bench.start(); +} diff --git a/doc/api/process.md b/doc/api/process.md index 2eac1160586af1..3907271bbfa26c 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -1461,13 +1461,11 @@ changes: * `callback` {Function} * `...args` {any} Additional arguments to pass when invoking the `callback` -The `process.nextTick()` method adds the `callback` to the "next tick queue". -Once the current turn of the event loop turn runs to completion, all callbacks -currently in the next tick queue will be called. - -This is *not* a simple alias to [`setTimeout(fn, 0)`][]. It is much more -efficient. It runs before any additional I/O events (including -timers) fire in subsequent ticks of the event loop. +`process.nextTick()` adds `callback` to the "next tick queue". This queue is +fully drained after the current operation on the JavaScript stack runs to +completion and before the event loop is allowed to continue. As a result, it's +possible to create an infinite loop if one were to recursively call +`process.nextTick()`. ```js console.log('start'); @@ -1542,11 +1540,6 @@ function definitelyAsync(arg, cb) { } ``` -The next tick queue is completely drained on each pass of the event loop -**before** additional I/O is processed. As a result, recursively setting -`nextTick()` callbacks will block any I/O from happening, just like a -`while(true);` loop. - ## process.noDeprecation