Converting arguments to an array

JavaScript performance comparison

Revision 9 of this test case created

Preparation code

<script>
  var SAFE_MODE = 0


  function EMPTY(args) {
    return SAFE_MODE ? [] : Array(args.length)
  }

  function arrayProtoSlice() {
    return Array.prototype.slice.apply(arguments);
  }

  function arrayProtoSliceExceptOne() {
    return Array.prototype.slice.call(arguments, 1);
  }

  function arrayProtoSliceExceptMany(many) {
    return Array.prototype.slice.call(arguments, many);
  }

  function arrayLiteralSlice() {
    return EMPTY(arguments).slice.apply(arguments);
  }

  function arrayLiteralSliceExceptOne() {
    return [].slice.call(arguments, 1);
  }

  function arrayLiteralSliceExceptMany(many) {
    return [].slice.call(arguments, many);
  }

  var _slice = [].slice;

  function closureSlice() {
    return _slice.call(arguments);
  }

  function closureSliceExceptOne() {
    return _slice.call(arguments, 1);
  }

  function closureSliceExceptMany(many) {
    return _slice.call(arguments, many);
  }

  var _splice = [].splice;

  function closureSplice() {
    return _splice.call(arguments);
  }

  function closureSpliceExceptOne() {
    return _splice.call(arguments, 1);
  }

  function closureSpliceExceptMany(many) {
    return _splice.call(arguments, many);
  }

  function slice() {
    return [].splice.call(arguments, 0);
  }

  function sliceExceptOne() {
    return [].splice.call(arguments, 1);
  }

  function sliceExceptMany(many) {
    return [].splice.call(arguments, many);
  }

  function splice() {
    return [].splice.call(arguments, 0);
  }

  function spliceExceptOne() {
    return [].splice.call(arguments, 1);
  }

  function spliceExceptMany(many) {
    return [].splice.call(arguments, many);
  }

  function push() {
    var x = [];
    x.push.apply(x, arguments);
    return x;
  }

  function pushLoop() {
    var x = [],
        len = arguments.length;
    for (var i = 0; i < len; ++i)
    x.push(arguments[i])
    return x;
  }

  function pushLoopExceptOne() {
    var x = [],
        len = arguments.length;
    for (var i = 1; i < len; ++i)
    x.push(arguments[i])
    return x;
  }

  function pushLoopExceptMany(many) {
    var x = [],
        len = arguments.length;
    for (var i = many; i < len; ++i)
    x.push(arguments[i])
    return x;
  }

  function unshift() {
    var x = [];
    x.unshift.apply(x, arguments);
    return x;
  }

  function unshiftExceptOne() {
    var x = [];
    x.unshift.apply(x, arguments);
    x.shift();
    return x;
  }

  function unshiftExceptMany(many) {
    var x = [];
    x.unshift.apply(x, arguments);
    x = x.splice(many)
    return x;
  }

  function whileDecPostfix() {
    var arr = EMPTY(arguments),
        i = arguments.length;
    while (i--) {
      arr[i] = arguments[i];
    }
    return arr;
  }

  function whileDecPostfixExceptOne() {
    var arr = EMPTY(arguments),
        i = arguments.length - 1;
    while (i) {
      arr[i] = arguments[--i];
    }
    return arr;
  }

  function whileDecPostfixExceptMany(many) {
    var arr = EMPTY(arguments),
        i = arguments.length,
        j = arguments.length - many;
    while (j > 0) {
      arr[--j] = arguments[--i]
    }
    return arr;
  }

  function whileInc() {
    var arr = EMPTY(arguments),
        i = 0,
        len = arguments.length;
    while (++i <= len) {
      arr[i] = arguments[i];
    }
    return arr;
  }

  function whileIncExceptOne() {
    var arr = [],
        i = 0,
        j, len = arguments.length;
    while ((j = i++) <= len) {
      arr[j] = arguments[i];
    }
    return arr;
  }

  function whileIncExceptMany(many) {
    var arr = [],
        i = many - 1,
        j = -1,
        len = arguments.length;
    while (i < len) {
      arr[++j] = arguments[++i];
    }
    return arr;
  }

  function byConstructor() {
    return Array.apply(Array, arguments);
  }

  var bigArray = new Array(1001).join("a").split("");
  var smallArray = new Array(6).join("a").split("");

  var manyArray = new Array(1001 + 44).join("a").split(""),
      many = 88;
  manyArray.unshift(many);
  var smallManyArray = new Array(7).join("a").split("");
  smallManyArray.unshift(1);

  SAFE_MODE = 0;

  arrayProtoSlice.apply(null, bigArray).length === bigArray.length || alert('arrayProtoSlice broken');
  arrayProtoSliceExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('arrayProtoSliceExceptOne broken');
  arrayProtoSliceExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('arrayProtoSliceExceptMany broken');
  arrayLiteralSlice.apply(null, bigArray).length === bigArray.length || alert('arrayLiteralSlice broken');
  arrayLiteralSliceExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('arrayLiteralSliceExceptOne broken');
  arrayLiteralSliceExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('arrayLiteralSliceExceptMany broken');
  closureSlice.apply(null, bigArray).length === bigArray.length || alert('closureSlice broken');
  closureSliceExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('closureSliceExceptOne broken');
  closureSliceExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('closureSliceExceptMany broken');
  closureSplice.apply(null, bigArray).length === bigArray.length || alert('closureSplice broken');
  closureSpliceExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('closureSpliceExceptOne broken');
  closureSpliceExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('closureSpliceExceptMany broken');
  slice.apply(null, bigArray).length === bigArray.length || alert('slice broken');
  sliceExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('sliceExceptOne broken');
  sliceExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('sliceExceptMany broken');
  splice.apply(null, bigArray).length === bigArray.length || alert('splice broken');
  spliceExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('spliceExceptOne broken');
  spliceExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('spliceExceptMany broken');
  push.apply(null, bigArray).length === bigArray.length || alert('push broken');
  pushLoop.apply(null, bigArray).length === bigArray.length || alert('pushLoop broken');
  pushLoopExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('pushLoopExceptOne broken');
  pushLoopExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('pushLoopExceptMany broken');
  unshift.apply(null, bigArray).length === bigArray.length || alert('unshift broken');
  unshiftExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('unshiftExceptOne broken');
  unshiftExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('unshiftExceptMany broken');
  whileDecPostfix.apply(null, bigArray).length === bigArray.length || alert('whileDecPostfix broken');
  whileDecPostfixExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('whileDecPostfixExceptOne broken');
  whileDecPostfixExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('whileDecPostfixExceptMany broken');
  whileInc.apply(null, bigArray).length === bigArray.length || alert('whileInc broken');
  whileIncExceptOne.apply(null, bigArray).length + 1 === bigArray.length || alert('whileIncExceptOne broken');
  whileIncExceptMany.apply(null, manyArray).length + many === bigArray.length || alert('whileIncExceptMany broken');
  byConstructor.apply(null, bigArray).length === bigArray.length || alert('byConstructor broken');

  if (console && console.log) console.log("gtg")
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Array prototype slice
arrayProtoSlice.apply(null, bigArray);
arrayProtoSlice.apply(null, smallArray);
pending…
Array literal slice
arrayLiteralSlice.apply(null, bigArray);
arrayLiteralSlice.apply(null, smallArray);
pending…
Array push
push.apply(null, bigArray);
push.apply(null, smallArray);
pending…
Array splice
splice.apply(null, bigArray);
splice.apply(null, smallArray);
pending…
Unshift
unshift.apply(null, bigArray);
unshift.apply(null, smallArray);
pending…
Closure
closureSlice.apply(null, bigArray);
closureSlice.apply(null, smallArray);
pending…
While loop
whileDecPostfix.apply(null, bigArray);
whileDecPostfix.apply(null, smallArray);
pending…
Constructor loop
byConstructor.apply(null, bigArray);
byConstructor.apply(null, smallArray);
pending…
arrayProtoSliceExceptOne
arrayProtoSliceExceptOne.apply(null, bigArray);
arrayProtoSliceExceptOne.apply(null, smallArray);
pending…
arrayProtoSliceExceptMany
arrayProtoSliceExceptMany.apply(null, manyArray);
arrayProtoSliceExceptMany.apply(null, smallManyArray);
pending…
arrayLiteralSliceExceptOne
arrayLiteralSliceExceptOne.apply(null, bigArray);
arrayLiteralSliceExceptOne.apply(null, smallArray);
pending…
arrayLiteralSliceExceptMany
arrayLiteralSliceExceptMany.apply(null, manyArray);
arrayLiteralSliceExceptMany.apply(null, smallManyArray);
pending…
closureSliceExceptOne
closureSliceExceptOne.apply(null, bigArray);
closureSliceExceptOne.apply(null, smallArray);
pending…
closureSliceExceptMany
closureSliceExceptMany.apply(null, manyArray);
closureSliceExceptMany.apply(null, smallManyArray);
pending…
closureSplice
closureSplice.apply(null, bigArray)
closureSplice.apply(null, smallArray)
pending…
closureSpliceExceptOne
closureSpliceExceptOne.apply(null, bigArray)
closureSpliceExceptOne.apply(null, smallArray)
pending…
closureSpliceExceptMany
closureSpliceExceptMany.apply(null, manyArray)
closureSpliceExceptMany.apply(null, smallManyArray)
pending…
slice
slice.apply(null, bigArray)
slice.apply(null, smallArray)
pending…
sliceExceptOne
sliceExceptOne.apply(null, bigArray)
sliceExceptOne.apply(null, smallArray)
pending…
sliceExceptMany
sliceExceptMany.apply(null, manyArray)
sliceExceptMany.apply(null, smallManyArray)
pending…
spliceExceptOne
spliceExceptOne.apply(null, manyArray)
spliceExceptOne.apply(null, smallManyArray)
pending…
pushLoop
pushLoop.apply(null, bigArray)
pushLoop.apply(null, smallArray)
pending…
pushLoopExceptOne
pushLoopExceptOne.apply(null, bigArray)
pushLoopExceptOne.apply(null, smallArray)
pending…
pushLoopExceptMany
pushLoopExceptMany.apply(null, manyArray)
pushLoopExceptMany.apply(null, smallManyArray)
pending…
unshiftExceptOne
unshiftExceptOne.apply(null, bigArray)
unshiftExceptOne.apply(null, smallArray)
pending…
unshiftExceptMany
unshiftExceptMany.apply(null, manyArray)
unshiftExceptMany.apply(null, smallManyArray)
pending…
whileDecPostfix
whileDecPostfix.apply(null, bigArray)
whileDecPostfix.apply(null, smallArray)
pending…
whileDecPostfixExceptOne
whileDecPostfixExceptOne.apply(null, bigArray)
whileDecPostfixExceptOne.apply(null, smallArray)
pending…
whileDecPostfixExceptMany
whileDecPostfixExceptMany.apply(null, manyArray)
whileDecPostfixExceptMany.apply(null, smallManyArray)
pending…
whileInc
whileInc.apply(null, bigArray)
whileInc.apply(null, smallArray)
pending…
whileIncExceptOne
whileIncExceptOne.apply(null, bigArray)
whileIncExceptOne.apply(null, smallArray)
pending…
whileIncExceptMany
whileIncExceptMany.apply(null, manyArray)
whileIncExceptMany.apply(null, smallManyArray)
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. Here’s a list of current revisions for this page:

0 comments

Add a comment