Skip to content

Instantly share code, notes, and snippets.

@seykron
Last active December 23, 2015 02:28
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 seykron/6566725 to your computer and use it in GitHub Desktop.
Save seykron/6566725 to your computer and use it in GitHub Desktop.
Test for JavaScript single-thread event loop.
var MAX = 5;
var INTERVAL = 250;
var EXPECTED_TIME = INTERVAL * MAX;
var intervalId;
var index = 0;
var totalTime = 0;
var lastTime = Date.now();
var longOperation = function () {
totalTime += Date.now() - lastTime;
lastTime = Date.now();
index += 1;
while (Date.now() - lastTime < INTERVAL * 2) {
// Do something looooooong.
}
if (index === MAX) {
console.log("Total time: " + totalTime + " ms");
console.log("Expected time: " + EXPECTED_TIME);
clearInterval(intervalId);
}
};
intervalId = setInterval(longOperation, INTERVAL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment