Skip to content

Instantly share code, notes, and snippets.

@NealEhardt NealEhardt/testTimeout.js
Last active Dec 2, 2016

Embed
What would you like to do?
Instrumentation for the event loop
/*
Paste this in the console of a live site. Or in your source somewhere that will run exactly once.
Logs warnings to the console like this:
Lagged for 254 ms over 2 loop(s). Should have taken 100 ms (39% of actual).
*/
(function (timeoutDurationMs) {
var lastQuickDate = null, lastDate = null, loopsSinceLastQuickDate = 0;
function testTimeout() {
var d = new Date();
if (!lastQuickDate) {
lastQuickDate = lastDate = d;
}
if (d - lastDate < timeoutDurationMs * 2) {
if (lastDate !== lastQuickDate) {
var slowdown = lastDate - lastQuickDate;
var expectedTime = loopsSinceLastQuickDate * timeoutDurationMs;
var percent = Math.round(expectedTime / slowdown * 100);
console.warn('Lagged for ' + slowdown + ' ms over ' + loopsSinceLastQuickDate
+ ' loop(s). Should have taken ' + expectedTime + ' ms (' + percent + '% of actual).');
}
lastQuickDate = d;
loopsSinceLastQuickDate = 0;
} else {
loopsSinceLastQuickDate++;
}
lastDate = d;
setTimeout(testTimeout, timeoutDurationMs);
}
testTimeout();
return 'Okay';
})(50);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.