setInterval vs. setTimeout

Benchmark created by Ryan Grove on


Description

Simple comparison of initialization overhead for setInterval vs. setTimeout for a repeating timer. Since jsPerf doesn't currently do async testing, this test doesn't show the actual cost of executing the timer callbacks.

Test runner

Ready to run.

Testing in
TestOps/sec
setInterval
var a = 0;

setInterval(function() {
 a += 1;
}, 20);
ready
setTimeout
var a = 0;

setTimeout(function() {
 a += 1;
 setTimeout(arguments.callee, 20);
}, 20);
ready
setTimeout w/predefined function
var a = 0;

function tick() {
 a += 1;
 setTimeout(tick, 20);
}

setTimeout(tick, 20);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.