Here's a simple slowdowns detection technique for javascript. Concept is very easy: basically just compares the expected timestamp of a function to the actual function execution's timestamp.
currentTimestamp - expectedTimestamp = delay;
For example if you got a delay of 1000ms compared to the expected timestamp, user is may experiencing slowdowns
With this I believe you can achieve what you want. Actually you can even build a fully functioning profiler with this method, keeping track of what is executed in that period of time... but that only for dev purposes, since the profiler itself would consume resources (webworkers would help, though)
Note: that's not a reliable method and browsers may issue false positives. For example on Chrome if you blur a tab, JS' performance slows down and you may get delays of seconds even if they didn't actually happen.
So you have to do something like this:
window.addEventListener("focus", function(){ slowdownsChecker.enable(); });
window.addEventListener("blur", function(){ slowdownsChecker.disable(); });
jsfiddle demo: http://jsfiddle.net/sAKyS/
here's the pseudo code just for to demostrate how it works
JS:
var expected = new Date().getTime() + 200;
var highestDelay = 0;
var delayDisplay = document.getElementById("delay");
var highestDelayDisplay = document.getElementById("hdelay");
function check(){
var delay = new Date().getTime() - expected;
delay = delay >= 0 ? delay : 0;
if(delay > highestDelay) highestDelay = delay;
delayDisplay.textContent = delay;
highestDelayDisplay.textContent = highestDelay;
expected = new Date().getTime() + 200;
}
setInterval(check, 200);
// ignore next code, it's just some heavy code for to demonstrate how it's working
var log = document.getElementById("log");
var button = document.getElementsByTagName("button")[0];
button.addEventListener("click", function(){
// prime numbers calculation
var sieve = [], i, j, max = 100000;
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
log.textContent += " - " + i;
for (j = i << 1; j <= max; j += i) {
sieve[j] = true;
}
}
}
});
HTML:
<div>current delay in ms: <span id="delay"></span></div>
<div>highest registered delay in ms: <span id="hdelay"></span></div>
<button>Start random computation</button>
<div id="log"></div>