Array.prototype.slice.call() vs [].slice.call()

JavaScript performance comparison

Revision 35 of this test case created by sk

Preparation code

<script>
  var items = [1, 2, 3, 4, 5];
  var slice = Array.prototype.slice;
  var globalObj = {
    slice: Array.prototype.slice,
    forSlice: function(inArr) {
      var length = inArr.length, i, arr = new Array(length);

      for (i = 0; i < length; i += 1) {
          arr[i] = inArr[i];
      }

      return arr;
    },
  };
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Array.prototype.slice
var arr = Array.prototype.slice.call(items);
pending…
[].slice
var arr = [].slice.call(items);
pending…
cached prototype
var arr = slice.call(items);
pending…
Instance method
var arr = items.slice();
pending…
elementwise copy
var length = items.length, i, arr = new Array(length);

for (i = 0; i < length; i += 1) {
    arr[i] = items[i];
}
pending…
under a global object
var arr = globalObj.slice.call(items);
pending…
for loop under a global object
var arr = globalObj.forSlice(items);
pending…
elementwise copy with 8 steps per loop
var length = items.length,
    l = length - (length % 8),
    i,
    arr = new Array(length);

for (i = 0; i < l; i += 1) {
    arr[i] = items[i];
    arr[++i] = items[i];
    arr[++i] = items[i];
    arr[++i] = items[i];
    arr[++i] = items[i];
    arr[++i] = items[i];
    arr[++i] = items[i];
    arr[++i] = items[i];
}

for (i; i < length; i += 1) {
    arr[i] = items[i];
}
pending…
elementwise copy with 4 steps per loop
var length = items.length,
    l = length - (length % 4),
    i,
    arr = new Array(length);

for (i = 0; i < l; i += 1) {
    arr[i] = items[i];
    arr[++i] = items[i];
    arr[++i] = items[i];
    arr[++i] = items[i];
}

for (i; i < length; i += 1) {
    arr[i] = items[i];
}
pending…

Compare results of other browsers

Revisions

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

0 Comments