Skip to content

Instantly share code, notes, and snippets.

@xk
Last active December 11, 2016 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xk/eb2e75c11ef0435a43438a373f8c0113 to your computer and use it in GitHub Desktop.
Save xk/eb2e75c11ef0435a43438a373f8c0113 to your computer and use it in GitHub Desktop.
node.js timers are borken
// 2016-12-10 jorge@jorgechamorro.com
// node.js timers are borken: node checktimers.js errs all the time
// But this won't ever err in a browser: copy-paste in any browser's console
// Update: somewhere between node 0.10.26 and 0.12.0 this has been fixed
(function (ctr,flag,errors) {
function _1 () { flag= 0 }
function _2 () { errors+= flag }
function _3 () { flag= 1 }
function _4 () { errors+= !flag }
(function loop (t,i) {
t= 5, i= 20, ctr+= i*4;
while (i--) {
setTimeout(_1, t+=1);
setTimeout(_2, t+=1);
setTimeout(_3, t+=1);
setTimeout(_4, t+=1);
}
setTimeout(loop, t+=20);
})();
(function logger (str,percent) {
setTimeout(logger, 333);
percent= (100 * errors / ctr).toFixed(2);
str= '['+ ctr+ '] ERRORS: '+ errors+ ' ['+ percent+ '% WRONG ORDER]\r';
this.navigator ?
console.log(str) :
process.stdout.write(str);
})();
})(0,0,0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment