Created
March 18, 2015 14:57
-
-
Save gkucmierz/151b27561e0f3c45e016 to your computer and use it in GitHub Desktop.
Lag measure
This file contains 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
(function() { | |
var lastTime = +new Date(); | |
var avgArr = []; | |
var avg; | |
setInterval(function() { | |
var time = +new Date(); | |
var diff = time - lastTime; | |
lastTime = time; | |
if (avg) { | |
if (diff > avg * 2) { | |
console.log('lag: ', diff + ' ms'); | |
} | |
} else { | |
avgArr.push(diff); | |
if (avgArr.length >= 100) { | |
avg = avgArr.reduce(function(acc, b) { | |
return acc + b; | |
}, 0) / avgArr.length; | |
console.log('avg: ', avg); | |
} | |
} | |
}, 0); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment