Converting arguments to an array (v8)

Revision 8 of this benchmark created by rektide on


Preparation HTML

<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

Ready to run.

Testing in
TestOps/sec
Array prototype slice
arrayProtoSlice.apply(null, bigArray);
arrayProtoSlice.apply(null, smallArray);
ready
Array literal slice
arrayLiteralSlice.apply(null, bigArray);
arrayLiteralSlice.apply(null, smallArray);
ready
Array push
push.apply(null, bigArray);
push.apply(null, smallArray);
ready
Array splice
splice.apply(null, bigArray);
splice.apply(null, smallArray);
ready
Unshift
unshift.apply(null, bigArray);
unshift.apply(null, smallArray);
ready
Closure
closureSlice.apply(null, bigArray);
closureSlice.apply(null, smallArray);
ready
While loop
whileDecPostfix.apply(null, bigArray);
whileDecPostfix.apply(null, smallArray);
ready
Constructor loop
byConstructor.apply(null, bigArray);
byConstructor.apply(null, smallArray);
ready
arrayProtoSliceExceptOne
arrayProtoSliceExceptOne.apply(null, bigArray);
arrayProtoSliceExceptOne.apply(null, smallArray);
ready
arrayProtoSliceExceptMany
arrayProtoSliceExceptMany.apply(null, manyArray);
arrayProtoSliceExceptMany.apply(null, smallManyArray);
ready
arrayLiteralSliceExceptOne
arrayLiteralSliceExceptOne.apply(null, bigArray);
arrayLiteralSliceExceptOne.apply(null, smallArray);
ready
arrayLiteralSliceExceptMany
arrayLiteralSliceExceptMany.apply(null, manyArray);
arrayLiteralSliceExceptMany.apply(null, smallManyArray);
ready
closureSliceExceptOne
closureSliceExceptOne.apply(null, bigArray);
closureSliceExceptOne.apply(null, smallArray);
 
ready
closureSliceExceptMany
closureSliceExceptMany.apply(null, manyArray);
closureSliceExceptMany.apply(null, smallManyArray);
 
ready
closureSplice
closureSplice.apply(null, bigArray)
closureSplice.apply(null, smallArray)
ready
closureSpliceExceptOne
closureSpliceExceptOne.apply(null, bigArray)
closureSpliceExceptOne.apply(null, smallArray)
ready
closureSpliceExceptMany
closureSpliceExceptMany.apply(null, manyArray)
closureSpliceExceptMany.apply(null, smallManyArray)
ready
slice
slice.apply(null, bigArray)
slice.apply(null, smallArray)
ready
sliceExceptOne
sliceExceptOne.apply(null, bigArray)
sliceExceptOne.apply(null, smallArray)
ready
sliceExceptMany
sliceExceptMany.apply(null, manyArray)
sliceExceptMany.apply(null, smallManyArray)
ready
spliceExceptOne
spliceExceptOne.apply(null, manyArray)
spliceExceptOne.apply(null, smallManyArray)
 
ready
pushLoop
pushLoop.apply(null, bigArray)
pushLoop.apply(null, smallArray)
ready
pushLoopExceptOne
pushLoopExceptOne.apply(null, bigArray)
pushLoopExceptOne.apply(null, smallArray)
 
ready
pushLoopExceptMany
pushLoopExceptMany.apply(null, manyArray)
pushLoopExceptMany.apply(null, smallManyArray)
 
ready
unshiftExceptOne
unshiftExceptOne.apply(null, bigArray)
unshiftExceptOne.apply(null, smallArray)
ready
unshiftExceptMany
unshiftExceptMany.apply(null, manyArray)
unshiftExceptMany.apply(null, smallManyArray)
ready
whileDecPostfix
whileDecPostfix.apply(null, bigArray)
whileDecPostfix.apply(null, smallArray)
ready
whileDecPostfixExceptOne
whileDecPostfixExceptOne.apply(null, bigArray)
whileDecPostfixExceptOne.apply(null, smallArray)
ready
whileDecPostfixExceptMany
whileDecPostfixExceptMany.apply(null, manyArray)
whileDecPostfixExceptMany.apply(null, smallManyArray)
ready
whileInc
whileInc.apply(null, bigArray)
whileInc.apply(null, smallArray)
ready
whileIncExceptOne
whileIncExceptOne.apply(null, bigArray)
whileIncExceptOne.apply(null, smallArray)
ready
whileIncExceptMany
whileIncExceptMany.apply(null, manyArray)
whileIncExceptMany.apply(null, smallManyArray)
ready

Revisions

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