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
When aliasing useful methods (Array.prototype.slice
, anyone) you can either:
Bind a variable to the name and then .call
or .apply
it:
<span class="kw2">var</span> slice <span class="sy0">=</span> Array.<span class="me1">prototype</span>.<span class="me1">slice</span><span class="sy0">;</span><br><span class="co1">// And then later:</span><br>slice.<span class="me1">call</span><span class="br0">(</span>arguments<span class="br0">)</span><span class="sy0">;</span>
Or bind Function.prototype.call
to the method, allowing you to treat it like a function again:
<span class="kw2">var</span> fcall <span class="sy0">=</span> <span class="kw2">Function</span>.<span class="me1">prototype</span>.<span class="me1">call</span><span class="sy0">,</span><br> slice <span class="sy0">=</span> fcall.<span class="me1">bind</span><span class="br0">(</span>Array.<span class="me1">prototype</span>.<span class="me1">slice</span><span class="br0">)</span><span class="sy0">;</span><br><span class="co1">// And then later:</span><br>slice<span class="br0">(</span>arguments<span class="br0">)</span><span class="sy0">;</span>
The question is, do we pay a performance penalty for the cleaner syntax?
var applicativeSlice = Array.prototype.slice,
callableSlice = Function.prototype.call.bind(Array.prototype.slice);
function applicativeToArray() {
return applicativeSlice.call(arguments);
}
function callableToArray() {
return callableSlice(arguments);
}
Ready to run.
Test | Ops/sec | |
---|---|---|
[].slice.call |
| ready |
Function.fn.call.bind([].slice) |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.