Skip to content

Instantly share code, notes, and snippets.

@xk
Forked from bigeasy/Output on Fedora 16
Created November 4, 2012 09:58
Show Gist options
  • Save xk/4011122 to your computer and use it in GitHub Desktop.
Save xk/4011122 to your computer and use it in GitHub Desktop.
Node.js Millisecond Timeout
$ node timers.js
setInterval(ƒ,1) -> (µs) :
[ 1477,
1172,
1071,
1084,
1122,
1145,
1064,
1062,
1094,
1130,
1125,
1125,
1125,
1124,
1123,
1124,
1129,
1123,
1094,
1124,
1089,
1125,
1157,
1185,
1150,
1159,
1156,
1167,
1148,
1146,
1148,
1147,
1154,
1148,
1062,
1145,
1091,
1138,
1123,
1115,
1118,
1156,
1167,
1116,
1107,
1123,
1123,
1125,
1123,
1056,
1130,
1131,
1125,
1129,
1123,
1125,
1124,
1123,
1123,
1124,
1124,
1124,
1130,
1077,
1165,
1138,
1127,
1060,
1124,
1124,
1086,
1138,
1123,
1124,
1124,
1089,
1121,
1029,
1114,
1098,
1136,
1156,
1149,
1085,
1114,
1145,
1147,
1074,
1094,
1153,
1149,
1109,
1122,
1124,
1124,
1058,
1128,
1145,
1146 ]
setTimeout(ƒ,2) -> (µs) :
[ 11948,
5510,
2412,
2238,
2177,
2220,
2247,
2256,
2215,
2223,
2236,
2244,
2226,
2264,
2131,
2149,
2084,
2116,
2206,
2164,
2263,
2256,
2207,
2163,
2227,
2226,
2226,
3470,
2223,
2239,
2174,
2267,
2224,
2252,
3716,
2239,
2286,
2133,
2220,
2234,
2229,
2255,
2225,
2274,
2614,
2396,
2242,
2244,
2268,
3232,
2249,
2186,
2152,
2247,
2229,
2244,
2172,
2227,
3283,
2228,
2237,
2226,
3247,
2255,
2178,
2188,
2884,
2712,
2211,
2215,
3170,
2288,
2266,
2228,
3241,
2217,
2214,
2248,
3235,
2252,
2236,
2244,
3238,
2241,
2252,
2148,
2225,
2128,
2233,
2221,
2243,
3231,
2209,
2234,
2241,
3244,
2255,
2206,
2183 ]
setTimeout(ƒ,1) -> (µs) :
[ 5898,
367,
26,
7,
4,
10,
3,
3,
3,
3,
3,
9,
3,
3,
3,
3,
3,
8,
3,
3,
3,
3,
8,
3,
3,
3,
3,
8,
3,
5,
304,
5,
13,
10,
3657,
33,
5,
5,
4,
4,
4,
12,
5,
4,
11,
5,
5,
12,
4,
7,
9,
3,
3,
8,
4,
3,
3,
3,
3,
8,
3,
3,
3,
3,
3,
10,
146,
81,
4,
12,
3,
3,
3,
3,
3,
8,
3,
3,
3,
3,
3,
3,
8,
3,
3,
3,
8,
3,
3,
3,
3,
3,
8,
3,
3,
3,
2157,
14,
4 ]
setTimeout(ƒ,0) -> (µs) :
[ 1663,
2608,
12,
3,
11,
3,
3,
3,
3,
3,
8,
3,
3,
3,
3,
3,
8,
3,
3,
7,
3,
3,
8,
3,
3,
3,
2,
3,
8,
3,
5,
3,
2,
2,
8,
3,
2,
3,
2,
2,
3,
8,
3,
3,
3,
2,
8,
3,
2,
3,
3,
3,
8,
3,
3,
2,
2,
2,
7,
3,
2,
3,
2,
2,
5,
3,
7,
3,
2,
2,
2,
8,
3,
3,
3,
5,
3,
8,
3,
3,
4,
3,
2,
4,
3,
4,
3,
2,
8,
3,
3,
2,
3,
3,
7,
3,
5,
3,
3 ]
nextTick(ƒ) -> (µs) :
[ 1131,
65,
21,
18,
11,
2,
4,
2,
1,
3,
2,
2,
3,
2,
1,
3,
2,
2,
3,
2,
7,
3,
2,
1,
3,
2,
2,
3,
2,
2,
3,
2,
2,
3,
2,
2,
3,
2,
1,
4,
2,
1,
3,
2,
2,
3,
5,
2,
3,
2,
2,
3,
2,
1,
8,
2,
1,
3,
2,
1,
3,
2,
2,
3,
2,
1,
3,
2,
1,
3,
2,
1,
8,
2,
1,
3,
2,
2,
3,
2,
2,
3,
2,
2,
3,
7,
2,
3,
2,
1,
3,
2,
3,
3,
2,
1,
3,
2,
2 ]
//[email protected] 2012-11-04
var epoch = process.hrtime();
var howMany= 99;
interval();
function interval () {
var intervals = [], interval = setInterval(function () {
if (intervals.length < howMany) {
intervals.push(µs(process.hrtime(epoch)));
epoch = process.hrtime();
} else {
clearInterval(interval);
//console.log(intervals.slice(intervals.length - 4));
console.log("setInterval(ƒ,1) -> (µs) :\n", intervals);
timeout(2, function () { timeout(1, function () { timeout(0, tick) }) });
}
}, 1);
}
function timeout (wait, callback) {
var setTimeouts = [];
function timeout () {
if (setTimeouts.length < howMany) {
setTimeouts.push(µs(process.hrtime(epoch)));
epoch = process.hrtime();
setTimeout(timeout, wait);
} else {
console.log("setTimeout(ƒ,"+ wait+ ") -> (µs) :\n", setTimeouts);
callback();
}
}
timeout();
}
var nextTicks = [];
function tick () {
if (nextTicks.length < howMany) {
nextTicks.push(µs(process.hrtime(epoch)));
epoch = process.hrtime();
process.nextTick(tick);
} else {
console.log("nextTick(ƒ) -> (µs) :\n", nextTicks);
}
}
function µs (t) {
return Math.round(t[1]/1e3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment