Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

faster with negative start args

  • Loading branch information...
commit 06c735a5491845e213824a1b5ad6bb372dee9fc6 1 parent 36b1700
Aaron Heckmann authored

Showing 4 changed files with 103 additions and 7 deletions. Show diff stats Hide diff stats

  1. +28 4 README.md
  2. +64 0 bench.js
  3. +6 3 lib/sliced.js
  4. +5 0 test/index.js
32 README.md
Source Rendered
@@ -7,12 +7,36 @@ A faster alternative to `[].slice.call(arguments)`.
7 7
8 8 Example output from [benchmark.js](https://github.com/bestiejs/benchmark.js)
9 9
10   - Array.prototype.slice.call x 1,289,592 ops/sec ±2.88% (87 runs sampled)
11   - [].slice.call x 1,345,451 ops/sec ±1.68% (97 runs sampled)
12   - cached slice.call x 10,719,886 ops/sec ±1.04% (99 runs sampled)
13   - sliced x 15,809,545 ops/sec ±1.46% (93 runs sampled)
  10 + Array.prototype.slice.call x 1,320,205 ops/sec ±2.35% (92 runs sampled)
  11 + [].slice.call x 1,314,605 ops/sec ±1.60% (95 runs sampled)
  12 + cached slice.call x 10,468,380 ops/sec ±1.45% (95 runs sampled)
  13 + sliced x 16,608,237 ops/sec ±1.40% (92 runs sampled)
14 14 fastest is sliced
15 15
  16 + Array.prototype.slice.call(arguments, 1) x 1,383,584 ops/sec ±1.73% (97 runs sampled)
  17 + [].slice.call(arguments, 1) x 1,494,735 ops/sec ±1.33% (95 runs sampled)
  18 + cached slice.call(arguments, 1) x 10,085,270 ops/sec ±1.51% (97 runs sampled)
  19 + sliced(arguments, 1) x 16,620,480 ops/sec ±1.29% (95 runs sampled)
  20 + fastest is sliced(arguments, 1)
  21 +
  22 + Array.prototype.slice.call(arguments, -1) x 1,303,262 ops/sec ±1.62% (94 runs sampled)
  23 + [].slice.call(arguments, -1) x 1,325,615 ops/sec ±1.36% (97 runs sampled)
  24 + cached slice.call(arguments, -1) x 9,673,603 ops/sec ±1.70% (96 runs sampled)
  25 + sliced(arguments, -1) x 16,384,575 ops/sec ±1.06% (91 runs sampled)
  26 + fastest is sliced(arguments, -1)
  27 +
  28 + Array.prototype.slice.call(arguments, -2, -10) x 1,404,390 ops/sec ±1.61% (95 runs sampled)
  29 + [].slice.call(arguments, -2, -10) x 1,514,367 ops/sec ±1.21% (96 runs sampled)
  30 + cached slice.call(arguments, -2, -10) x 9,836,017 ops/sec ±1.21% (95 runs sampled)
  31 + sliced(arguments, -2, -10) x 18,544,882 ops/sec ±1.30% (91 runs sampled)
  32 + fastest is sliced(arguments, -2, -10)
  33 +
  34 + Array.prototype.slice.call(arguments, -2, -1) x 1,458,604 ops/sec ±1.41% (97 runs sampled)
  35 + [].slice.call(arguments, -2, -1) x 1,536,547 ops/sec ±1.63% (99 runs sampled)
  36 + cached slice.call(arguments, -2, -1) x 10,060,633 ops/sec ±1.37% (96 runs sampled)
  37 + sliced(arguments, -2, -1) x 18,608,712 ops/sec ±1.08% (93 runs sampled)
  38 + fastest is sliced(arguments, -2, -1)
  39 +
16 40 _Benchmark [source](https://github.com/aheckmann/sliced/blob/master/bench.js)._
17 41
18 42 ##Usage
64 bench.js
@@ -19,6 +19,70 @@ s.add('Array.prototype.slice.call', function () {
19 19 })
20 20 .run();
21 21
  22 +var s = new Bench.Suite;
  23 +s.add('Array.prototype.slice.call(arguments, 1)', function () {
  24 + Array.prototype.slice.call(arguments, 1);
  25 +}).add('[].slice.call(arguments, 1)', function () {
  26 + [].slice.call(arguments, 1);
  27 +}).add('cached slice.call(arguments, 1)', function () {
  28 + slice.call(arguments, 1)
  29 +}).add('sliced(arguments, 1)', function () {
  30 + sliced(arguments, 1)
  31 +}).on('cycle', function (evt) {
  32 + console.log(String(evt.target));
  33 +}).on('complete', function () {
  34 + console.log('fastest is %s', this.filter('fastest').pluck('name'));
  35 +})
  36 +.run();
  37 +
  38 +var s = new Bench.Suite;
  39 +s.add('Array.prototype.slice.call(arguments, -1)', function () {
  40 + Array.prototype.slice.call(arguments, -1);
  41 +}).add('[].slice.call(arguments, -1)', function () {
  42 + [].slice.call(arguments, -1);
  43 +}).add('cached slice.call(arguments, -1)', function () {
  44 + slice.call(arguments, -1)
  45 +}).add('sliced(arguments, -1)', function () {
  46 + sliced(arguments, -1)
  47 +}).on('cycle', function (evt) {
  48 + console.log(String(evt.target));
  49 +}).on('complete', function () {
  50 + console.log('fastest is %s', this.filter('fastest').pluck('name'));
  51 +})
  52 +.run();
  53 +
  54 +var s = new Bench.Suite;
  55 +s.add('Array.prototype.slice.call(arguments, -2, -10)', function () {
  56 + Array.prototype.slice.call(arguments, -2, -10);
  57 +}).add('[].slice.call(arguments, -2, -10)', function () {
  58 + [].slice.call(arguments, -2, -10);
  59 +}).add('cached slice.call(arguments, -2, -10)', function () {
  60 + slice.call(arguments, -2, -10)
  61 +}).add('sliced(arguments, -2, -10)', function () {
  62 + sliced(arguments, -2, -10)
  63 +}).on('cycle', function (evt) {
  64 + console.log(String(evt.target));
  65 +}).on('complete', function () {
  66 + console.log('fastest is %s', this.filter('fastest').pluck('name'));
  67 +})
  68 +.run();
  69 +
  70 +var s = new Bench.Suite;
  71 +s.add('Array.prototype.slice.call(arguments, -2, -1)', function () {
  72 + Array.prototype.slice.call(arguments, -2, -1);
  73 +}).add('[].slice.call(arguments, -2, -1)', function () {
  74 + [].slice.call(arguments, -2, -1);
  75 +}).add('cached slice.call(arguments, -2, -1)', function () {
  76 + slice.call(arguments, -2, -1)
  77 +}).add('sliced(arguments, -2, -1)', function () {
  78 + sliced(arguments, -2, -1)
  79 +}).on('cycle', function (evt) {
  80 + console.log(String(evt.target));
  81 +}).on('complete', function () {
  82 + console.log('fastest is %s', this.filter('fastest').pluck('name'));
  83 +})
  84 +.run();
  85 +
22 86 /**
23 87 * Output:
24 88 *
9 lib/sliced.js
@@ -10,16 +10,19 @@
10 10
11 11 module.exports = function (args, slice, sliceEnd) {
12 12 var ret = [];
  13 + var len = args.length;
  14 +
  15 + if (0 === len) return ret;
13 16
14 17 var start = slice < 0
15   - ? slice + args.length
  18 + ? Math.max(0, slice + len)
16 19 : slice || 0;
17 20
18 21 var end = 3 === arguments.length
19 22 ? sliceEnd < 0
20   - ? sliceEnd + args.length
  23 + ? sliceEnd + len
21 24 : sliceEnd
22   - : args.length;
  25 + : len;
23 26
24 27 for (var i = start; i < end; ++i) {
25 28 ret[i - start] = args[i];
5 test/index.js
@@ -40,6 +40,11 @@ describe('sliced', function(){
40 40 assert.equal(2, r.length);
41 41 assert.equal(o[2], r[0]);
42 42 assert.equal(o[3], r[1]);
  43 +
  44 + var r = sliced(o, -12);
  45 + assert.equal(4, r.length);
  46 + assert.equal(o[0], r[0]);
  47 + assert.equal(o[1], r[1]);
43 48 })
44 49 })
45 50 describe('with negative start and positive end', function(){

0 comments on commit 06c735a

Please sign in to comment.
Something went wrong with that request. Please try again.