Smallest timeout value

JavaScript performance comparison

Revision 2 of this test case created by Thomas Aylott

Info

The smallest setTimeout timeout value allowed by the HTML5 specification is 4 ms. Smaller values should clamp to 4 ms.

Therefore, the first two tests below should have about the same result.

Preparation code

<script>
  var img = new Image;
 
 
 
  (function() {
 
  var img = new Image;
  var timeouts = [];
  var SRC = 'data:image/png;base64,';
 
  // 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 setZeroTimeoutWithImage(fn) {
      timeouts.push(fn);
      img.src = SRC;
  }
 
  function handleMessage(event) {
      event.stopPropagation();
      if (timeouts.length > 0) timeouts.shift()();
  }
 
  img.onReadyStateChange =
  img.onabort =
  img.onload =
  img.onerror =
  handleMessage;
 
  // Add the one thing we want added to the window object.
  window.setZeroTimeoutWithImage = setZeroTimeoutWithImage;
 
  })();
 
 
      // 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;
      })();
 
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
timeout = 0
// async test
setTimeout(function() {
  deferred.resolve();
}, 0);
pending…
timeout = 4
// async test
setTimeout(function() {
  deferred.resolve();
}, 4);
pending…
timeout = 10
// async test
setTimeout(function() {
  deferred.resolve();
}, 10);
pending…
timeout = 40
// async test
setTimeout(function() {
  deferred.resolve();
}, 40);
pending…
setZeroTimeout
// async test
setZeroTimeout(function() {
  deferred.resolve();
})
 
pending…
Image.onerror
// async test
img.onload = img.onerror = function() {
  deferred.resolve()
  return true
};
img.src = 'data:image/png;base64,';
 
pending…
setZeroTimeoutWithImage
// async test
setZeroTimeoutWithImage(function() {
  deferred.resolve();
})
 
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment