Arguments to array

Benchmark created by AAAA on


Setup

function arrayProtoSliceCall(args, skip) {
      return Array.prototype.slice.call(args, skip)
    }
    
    function whileLoopCall(args, skip) {
      var arr = [],
        i = args.length;
      while (i-- > skip) {
        arr[i] = args[i];
      }
      return arr.slice(skip);
    }
    
    function whileLoopCall2(args, skip) {
      var arr = [],
        i = args.length;
      while (i-- > skip) {
        arr[i - skip] = args[i];
      }
      return arr;
    }
    
    var f = {
      arrayProtoSliceCall: function() {
        return arrayProtoSliceCall(arguments, 1)
      },
      whileLoopCall: function() {
        return whileLoopCall(arguments, 1)
      },
      whileLoopCall2: function() {
        return whileLoopCall2(arguments, 1)
      },
      directCall: function() {
        var skip = 1;
        var arr = [],
          i = arguments.length;
        while (i-- > skip) {
          arr[i - skip] = arguments[i];
        }
        return arr;
      },
      directCallFor: function() {
        skip = 1;
        var i = arguments.length,
          result = new Array(i - skip);
        i--;
        for (; i >= skip; i--) result[i - skip] = arguments[i];
        return result;
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
arrayProtoSliceCall
f.arrayProtoSliceCall(1, 2, 3, 4, 5, 6, 7, 8, 9)
ready
whileLoopCall
f.whileLoopCall(1, 2, 3, 4, 5, 6, 7, 8, 9)
ready
whileLoopCall2
f.whileLoopCall2(1, 2, 3, 4, 5, 6, 7, 8, 9)
ready
directCallWhile
f.directCall(1, 2, 3, 4, 5, 6, 7, 8, 9)
ready
directCallFor
f.directCallFor(1, 2, 3, 4, 5, 6, 7, 8, 9)
ready

Revisions

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