public
Created

Node.js Millisecond Timeout

  • Download Gist
Output on Fedora 16
1 2 3 4 5
[ [ 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 ] ]
Output on OS X Mountain Lion
1 2 3 4 5
[ [ 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 ] ]
timers.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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));
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.