Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
Last active December 20, 2015 19:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBruant/6183351 to your computer and use it in GitHub Desktop.
Save DavidBruant/6183351 to your computer and use it in GitHub Desktop.
To understand the whole 4ms clamping situation
<!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>
@DavidBruant
Copy link
Author

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.

@mathiasbynens
Copy link

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