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
In this test we look at two techniques for applying Array
prototype functions to an array object:
Technique A: Array.prototype.slice.call(arr)
is the traditional approach, but can be cumbersome to type
Technique B: [].slice.call(arr)
is easier to type, but adds the overhead of creating an array instance each time.
So is B actually slower? If so, by how much?
Note: This version of the test is pretty much a complete rewrite of the Author's original test. I felt it was an interesting problem to look at, but with apologies to the OA, I don't think his version was actually doing what he thought, and wasn't really providing any insight.
The result: As expected, referencing Array.prototype
is faster than []
(by 1.3x on Chrome, 3x on Firefox, and 8x on Safari). But in the context of the slice()
operation, the difference is actually fairly negligible, < 10% difference on all browsers.
<script>
// Initialize src and dst vars
var src = 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.split(' '),
dst;
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
reference Array.protoype |
| ready |
reference [] |
| ready |
Array.prototype.slice |
| ready |
[].slice |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.