setInterval vs. setTimeout (v8)

Revision 8 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);
    }
  }, 0);
ready
setTimeout
var a = 0;

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

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

setTimeout(tick, 0);
ready
setInterval function
var a = 0,

  function bibi() {
    a += 1;
    if (a === 2) {
      clearInterval(b);
    }
  }

b = setInterval(bibi, 0);
ready

Revisions

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