Last active
December 23, 2015 02:28
-
-
Save seykron/6566725 to your computer and use it in GitHub Desktop.
Test for JavaScript single-thread event loop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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