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
Attempt to demonstrate the performance impact of having a closure in a function, even when that closure is never evaluated. Engines (at this point) allocate the space for the variables that the closure shares immediately when entering the function. By moving the closure into another function, the alternative test case ensures that it only pays this price when the closure is actually used.
function f_closure(x) {
if (x < 0) {
return [x].reduce(function(a, b) { return a + x + b; }, 0);
} else {
return x;
}
}
function make_closure(x) {
return function(a, b) { return a + x + b; };
}
function f_no_closure(x) {
if (x < 0) {
return [x].reduce(make_closure(x), 0);
} else {
return x;
}
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Closure in function |
| ready |
Closure external |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.