jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
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.
<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>
Ready to run.
Test | Ops/sec | |
---|---|---|
timeout = 0 |
| ready |
timeout = 4 |
| ready |
timeout = 10 |
| ready |
timeout = 40 |
| ready |
setZeroTimeout |
| ready |
Image.onerror |
| ready |
setZeroTimeoutWithImage |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.