setInterval vs. setTimeout (v5)

Revision 5 of this benchmark created 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,
    b = setInterval(function() {
            a += 1;
            if (a === 2) {
                clearInterval(b);
            }
        }, 1);
ready
setTimeout
var a = 0;

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

function tick() {
  a += 1;
  if (a !== 2) {
      setTimeout(tick, 1);
  }
}

setTimeout(tick, 1);
ready

Revisions

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