-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcount-spins.html
71 lines (59 loc) · 1.6 KB
/
count-spins.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
</head>
<body>
<script>
// BEGIN implementation of setZeroTimeout
// Only add setZeroTimeout to the window object, and hide everything
// else in a closure.
(function() {
var timeouts = [];
var messageName = "zero-timeout-message";
// Like setTimeout, but only takes a function argument. There's
// no time argument (always zero) and no arguments (you have to
// use a closure).
function setZeroTimeout(fn) {
timeouts.push(fn);
window.postMessage(messageName, "*");
}
function handleMessage(event) {
if (event.source == window && event.data == messageName) {
event.stopPropagation();
if (timeouts.length > 0) {
var fn = timeouts.shift();
fn();
}
}
}
window.addEventListener("message", handleMessage, true);
// Add the one thing we want added to the window object.
window.setZeroTimeout = setZeroTimeout;
})();
// END implementation of setZeroTimeout
const COUNT = 10000;
function appendBody(x) {
const e = document.createElement('div');
e.innerHTML = x;
document.body.appendChild(e);
}
const start = performance.now();
function boom() {
const diff = performance.now() - start;
appendBody(COUNT + ' spins in ' + (diff) + 'ms');
appendBody('' + (diff / COUNT * 1000) + 'us per spin');
}
let i = COUNT;
function tick() {
i -= 1;
if (i === 0) {
boom();
return;
}
setZeroTimeout(tick);
}
setZeroTimeout(tick);
</script>
</body>
</html>