Use setImmediate if you want to queue the function behind whatever I/O event callbacks that are already in the event queue.
Use process.nextTick to effectively queue the function at the head of the event queue so that it executes immediately after the current function completes. It's slightly faster than setTimeout:
'use strict'
var bench = require('fastbench')
var run = bench([
function benchSetImmediate (done) {
setImmediate(done)
},
function benchNextTick (done) {
process.nextTick(done)
}
], 1024 * 1024)
// run them two times
run(run)
Considerations on browser side
on browser side, setImmediate/process.nextTick is not available.
Use setImmediate if you want to queue the function behind whatever I/O event callbacks that are already in the event queue.
Use process.nextTick to effectively queue the function at the head of the event queue so that it executes immediately after the current function completes. It's slightly faster than setTimeout:
Considerations on browser side
setTimeout(fn, 0)
.