Last active
December 20, 2015 19:29
-
-
Save DavidBruant/6183351 to your computer and use it in GitHub Desktop.
To understand the whole 4ms clamping situation
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<script> | |
var MAX = 10; | |
var i = 0; | |
var times = []; | |
var now = performance.now.bind(performance) | |
setTimeout(function f(){ | |
times[i] = now(); | |
if(i < MAX){ | |
setTimeout(f, 0); | |
i++; | |
} | |
else{ | |
console.log( times.reduce(function(acc, curr, i){ | |
if(i === 0) | |
return acc; | |
acc.push(curr - times[i-1]) | |
return acc; | |
}, []) ); | |
} | |
}, 100); | |
</script> | |
<title> TITLE </title> | |
</head> | |
<body> | |
</body> | |
</html> |
Background story: last week, @DavidBruant’s tweet made me wonder… If setTimeout(f, 0)
actually runs as soon as possible (no 4ms clamping), then why is the LawnMark benchmark affected by it?
Online version: https://rawgithub.com/DavidBruant/6183351/raw/e44545406b3fa125e8ff7e0453226abb767ec2d2/setTimeoutClamping.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Open this in a browser. In your console, you should notice that the 4 first timestamps are really close to one another (as close to what the browser can make them). Each following timestamp is separated from at least 4ms to the previous one.