Skip to content

Commit

Permalink
faster with negative start args
Browse files Browse the repository at this point in the history
  • Loading branch information
aheckmann committed Sep 29, 2012
1 parent 36b1700 commit 06c735a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
32 changes: 28 additions & 4 deletions README.md
Expand Up @@ -7,12 +7,36 @@ A faster alternative to `[].slice.call(arguments)`.

Example output from [benchmark.js](https://github.com/bestiejs/benchmark.js)

Array.prototype.slice.call x 1,289,592 ops/sec ±2.88% (87 runs sampled)
[].slice.call x 1,345,451 ops/sec ±1.68% (97 runs sampled)
cached slice.call x 10,719,886 ops/sec ±1.04% (99 runs sampled)
sliced x 15,809,545 ops/sec ±1.46% (93 runs sampled)
Array.prototype.slice.call x 1,320,205 ops/sec ±2.35% (92 runs sampled)
[].slice.call x 1,314,605 ops/sec ±1.60% (95 runs sampled)
cached slice.call x 10,468,380 ops/sec ±1.45% (95 runs sampled)
sliced x 16,608,237 ops/sec ±1.40% (92 runs sampled)
fastest is sliced

Array.prototype.slice.call(arguments, 1) x 1,383,584 ops/sec ±1.73% (97 runs sampled)
[].slice.call(arguments, 1) x 1,494,735 ops/sec ±1.33% (95 runs sampled)
cached slice.call(arguments, 1) x 10,085,270 ops/sec ±1.51% (97 runs sampled)
sliced(arguments, 1) x 16,620,480 ops/sec ±1.29% (95 runs sampled)
fastest is sliced(arguments, 1)

Array.prototype.slice.call(arguments, -1) x 1,303,262 ops/sec ±1.62% (94 runs sampled)
[].slice.call(arguments, -1) x 1,325,615 ops/sec ±1.36% (97 runs sampled)
cached slice.call(arguments, -1) x 9,673,603 ops/sec ±1.70% (96 runs sampled)
sliced(arguments, -1) x 16,384,575 ops/sec ±1.06% (91 runs sampled)
fastest is sliced(arguments, -1)

Array.prototype.slice.call(arguments, -2, -10) x 1,404,390 ops/sec ±1.61% (95 runs sampled)
[].slice.call(arguments, -2, -10) x 1,514,367 ops/sec ±1.21% (96 runs sampled)
cached slice.call(arguments, -2, -10) x 9,836,017 ops/sec ±1.21% (95 runs sampled)
sliced(arguments, -2, -10) x 18,544,882 ops/sec ±1.30% (91 runs sampled)
fastest is sliced(arguments, -2, -10)

Array.prototype.slice.call(arguments, -2, -1) x 1,458,604 ops/sec ±1.41% (97 runs sampled)
[].slice.call(arguments, -2, -1) x 1,536,547 ops/sec ±1.63% (99 runs sampled)
cached slice.call(arguments, -2, -1) x 10,060,633 ops/sec ±1.37% (96 runs sampled)
sliced(arguments, -2, -1) x 18,608,712 ops/sec ±1.08% (93 runs sampled)
fastest is sliced(arguments, -2, -1)

_Benchmark [source](https://github.com/aheckmann/sliced/blob/master/bench.js)._

##Usage
Expand Down
64 changes: 64 additions & 0 deletions bench.js
Expand Up @@ -19,6 +19,70 @@ s.add('Array.prototype.slice.call', function () {
})
.run();

var s = new Bench.Suite;
s.add('Array.prototype.slice.call(arguments, 1)', function () {
Array.prototype.slice.call(arguments, 1);
}).add('[].slice.call(arguments, 1)', function () {
[].slice.call(arguments, 1);
}).add('cached slice.call(arguments, 1)', function () {
slice.call(arguments, 1)
}).add('sliced(arguments, 1)', function () {
sliced(arguments, 1)
}).on('cycle', function (evt) {
console.log(String(evt.target));
}).on('complete', function () {
console.log('fastest is %s', this.filter('fastest').pluck('name'));
})
.run();

var s = new Bench.Suite;
s.add('Array.prototype.slice.call(arguments, -1)', function () {
Array.prototype.slice.call(arguments, -1);
}).add('[].slice.call(arguments, -1)', function () {
[].slice.call(arguments, -1);
}).add('cached slice.call(arguments, -1)', function () {
slice.call(arguments, -1)
}).add('sliced(arguments, -1)', function () {
sliced(arguments, -1)
}).on('cycle', function (evt) {
console.log(String(evt.target));
}).on('complete', function () {
console.log('fastest is %s', this.filter('fastest').pluck('name'));
})
.run();

var s = new Bench.Suite;
s.add('Array.prototype.slice.call(arguments, -2, -10)', function () {
Array.prototype.slice.call(arguments, -2, -10);
}).add('[].slice.call(arguments, -2, -10)', function () {
[].slice.call(arguments, -2, -10);
}).add('cached slice.call(arguments, -2, -10)', function () {
slice.call(arguments, -2, -10)
}).add('sliced(arguments, -2, -10)', function () {
sliced(arguments, -2, -10)
}).on('cycle', function (evt) {
console.log(String(evt.target));
}).on('complete', function () {
console.log('fastest is %s', this.filter('fastest').pluck('name'));
})
.run();

var s = new Bench.Suite;
s.add('Array.prototype.slice.call(arguments, -2, -1)', function () {
Array.prototype.slice.call(arguments, -2, -1);
}).add('[].slice.call(arguments, -2, -1)', function () {
[].slice.call(arguments, -2, -1);
}).add('cached slice.call(arguments, -2, -1)', function () {
slice.call(arguments, -2, -1)
}).add('sliced(arguments, -2, -1)', function () {
sliced(arguments, -2, -1)
}).on('cycle', function (evt) {
console.log(String(evt.target));
}).on('complete', function () {
console.log('fastest is %s', this.filter('fastest').pluck('name'));
})
.run();

/**
* Output:
*
Expand Down
9 changes: 6 additions & 3 deletions lib/sliced.js
Expand Up @@ -10,16 +10,19 @@

module.exports = function (args, slice, sliceEnd) {
var ret = [];
var len = args.length;

if (0 === len) return ret;

var start = slice < 0
? slice + args.length
? Math.max(0, slice + len)
: slice || 0;

var end = 3 === arguments.length
? sliceEnd < 0
? sliceEnd + args.length
? sliceEnd + len
: sliceEnd
: args.length;
: len;

for (var i = start; i < end; ++i) {
ret[i - start] = args[i];
Expand Down
5 changes: 5 additions & 0 deletions test/index.js
Expand Up @@ -40,6 +40,11 @@ describe('sliced', function(){
assert.equal(2, r.length);
assert.equal(o[2], r[0]);
assert.equal(o[3], r[1]);

var r = sliced(o, -12);
assert.equal(4, r.length);
assert.equal(o[0], r[0]);
assert.equal(o[1], r[1]);
})
})
describe('with negative start and positive end', function(){
Expand Down

0 comments on commit 06c735a

Please sign in to comment.