Skip to content

Instantly share code, notes, and snippets.

@bigeasy
Created November 4, 2012 05:41
Show Gist options
  • Save bigeasy/4010472 to your computer and use it in GitHub Desktop.
Save bigeasy/4010472 to your computer and use it in GitHub Desktop.
Node.js Millisecond Timeout
[ [ 0, 1859987 ], [ 0, 1678600 ], [ 0, 1074869 ], [ 0, 1100038 ] ]
[ [ 0, 2997281 ], [ 0, 2231579 ], [ 0, 2200601 ], [ 0, 3107400 ] ]
[ [ 0, 2925 ], [ 0, 2907 ], [ 0, 2910 ], [ 0, 2860 ] ]
[ [ 0, 2869 ], [ 0, 2895 ], [ 0, 2800 ], [ 0, 2851 ] ]
[ [ 0, 1425 ], [ 0, 1925 ], [ 0, 1858 ], [ 0, 1383 ] ]
[ [ 0, 1225887 ], [ 0, 1260835 ], [ 0, 1190385 ], [ 0, 1239280 ] ]
[ [ 0, 2379501 ], [ 0, 3575475 ], [ 0, 2255718 ], [ 0, 2430142 ] ]
[ [ 0, 4968 ], [ 0, 2116 ], [ 0, 1921 ], [ 0, 1986 ] ]
[ [ 0, 1940 ], [ 0, 4828 ], [ 0, 2043 ], [ 0, 1943 ] ]
[ [ 0, 1042 ], [ 0, 1660 ], [ 0, 1246 ], [ 0, 1041 ] ]
var epoch = process.hrtime();
interval();
function interval () {
var intervals = [], interval = setInterval(function () {
if (intervals.length < 12) {
intervals.push(process.hrtime(epoch));
epoch = process.hrtime();
} else {
clearInterval(interval);
console.log(intervals.slice(intervals.length - 4));
timeout(2, function () { timeout(1, function () { timeout(0, tick) }) });
}
}, 1);
}
function timeout (wait, callback) {
var setTimeouts = [];
function timeout () {
if (setTimeouts.length < 12) {
setTimeouts.push(process.hrtime(epoch));
epoch = process.hrtime();
setTimeout(timeout, wait);
} else {
console.log(setTimeouts.slice(setTimeouts.length - 4));
callback();
}
}
timeout();
}
var nextTicks = [];
function tick () {
if (nextTicks.length < 12) {
nextTicks.push(process.hrtime(epoch));
epoch = process.hrtime();
process.nextTick(tick);
} else {
console.log(nextTicks.slice(nextTicks.length - 4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment