Truly independent web browser
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
// The timer initialization steps clamp a timeout below 4ms up to 4ms once the timer nesting level exceeds 5, and a
// setTimeout() call made from inside a timer callback inherits the callback's nesting level, so a chain of nested
// setTimeout(f, 0) calls slows to one hop per 4ms after its first few hops. A chain of 30 hops therefore takes at
// least 24 * 4ms = 96ms, while an unclamped one finishes in well under a millisecond.
asyncTest(done => {
const start = performance.now();
let hop = 0;
const step = () => {
if (++hop < 30) {
setTimeout(step, 0);
return;
}
const elapsed = performance.now() - start;
println(`A chain of 30 nested setTimeout(f, 0) calls takes at least 90ms: ${elapsed >= 90}`);
testChainThroughPromiseReactions();
};
setTimeout(step, 0);
// A chain whose every hop schedules the next one from a promise reaction, as an "await" on a promise that a
// 0ms timer resolves does, is clamped the same way: a microtask run in a timer callback's checkpoint carries
// the callback's nesting level.
function testChainThroughPromiseReactions() {
const start = performance.now();
let hop = 0;
const step = () => {
if (++hop < 30) {
Promise.resolve().then(() => setTimeout(step, 0));
return;
}
const elapsed = performance.now() - start;
println(`A chain of 30 setTimeout(f, 0) calls made from promise reactions takes at least 90ms: ${elapsed >= 90}`);
testTimerScheduledFromMicrotask();
};
setTimeout(step, 0);
}
// A setTimeout() call made during the microtask checkpoint after a deeply-nested timer callback is clamped
// like one the callback makes itself, so the two 0ms timers fire in the order they were scheduled.
function testTimerScheduledFromMicrotask() {
const order = [];
const record = name => {
order.push(name);
if (order.length === 2) {
println(`Timers fired in the order: ${order.join(", ")}`);
done();
}
};
let depth = 0;
const nest = () => {
if (++depth < 8) {
setTimeout(nest, 0);
return;
}
setTimeout(() => record("clamped timer from the callback"), 0);
queueMicrotask(() => setTimeout(() => record("clamped timer from the microtask"), 0));
};
setTimeout(nest, 0);
}
});
</script>