[] vs Array for object conversion (v2)

Revision 2 of this benchmark created by Robert K on


Description

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.

Preparation HTML

<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>

Test runner

Ready to run.

Testing in
TestOps/sec
reference Array.protoype
var dst = Array.prototype;
ready
reference []
var dst = [];
ready
Array.prototype.slice
var dst = Array.prototype.slice.call(src);
ready
[].slice
var dst = [].slice.call(src);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by FGRibreau on
  • Revision 2: published by Robert K on
  • Revision 7: published by Leonardo Dutra on
  • Revision 9: published by ecmanaut on