Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 50df0d6
Showing
10 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.sw* | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- 0.6 | ||
- 0.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
0.0.1 / 2012-09-29 | ||
=================== | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
test: | ||
@time ./node_modules/.bin/mocha $(T) $(TESTS) | ||
|
||
.PHONY: test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#sliced | ||
========== | ||
|
||
A faster alternative to `[].slice.call(arguments)`. | ||
|
||
[![Build Status](https://secure.travis-ci.org/aheckmann/sliced.png)](http://travis-ci.org/aheckmann/sliced) | ||
|
||
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) | ||
fastest is sliced | ||
|
||
_Benchmark [source](https://github.com/aheckmann/sliced/blob/master/bench.js)._ | ||
|
||
##Usage | ||
|
||
`sliced` accepts the same arguments as `Array#slice` so you can easily swap it out. | ||
|
||
```js | ||
function zing () { | ||
var slow = [].slice.call(arguments, 1, 8); | ||
var args = slice(arguments, 1, 8); | ||
|
||
var slow = Array.prototype.slice.call(arguments); | ||
var args = slice(arguments); | ||
// etc | ||
} | ||
``` | ||
|
||
## install | ||
|
||
npm install sliced | ||
|
||
|
||
[LICENSE](https://github.com/aheckmann/sliced/blob/master/LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
var sliced = require('./') | ||
var Bench = require('benchmark'); | ||
var s = new Bench.Suite; | ||
var slice = [].slice; | ||
|
||
s.add('Array.prototype.slice.call', function () { | ||
Array.prototype.slice.call(arguments); | ||
}).add('[].slice.call', function () { | ||
[].slice.call(arguments); | ||
}).add('cached slice.call', function () { | ||
slice.call(arguments) | ||
}).add('sliced', function () { | ||
sliced(arguments) | ||
}).on('cycle', function (evt) { | ||
console.log(String(evt.target)); | ||
}).on('complete', function () { | ||
console.log('fastest is %s', this.filter('fastest').pluck('name')); | ||
}) | ||
.run(); | ||
|
||
/** | ||
* Output: | ||
* | ||
* 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) | ||
* fastest is sliced | ||
* | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = exports = require('./lib/sliced'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
/** | ||
* An Array.prototype.slice.call(arguments) alternative | ||
* | ||
* @param {Object} args something with a length | ||
* @param {Number} slice | ||
* @param {Number} sliceEnd | ||
* @api public | ||
*/ | ||
|
||
module.exports = function (args, slice, sliceEnd) { | ||
var ret = []; | ||
var start = slice || 0; | ||
var end = 3 === arguments.length | ||
? sliceEnd | ||
: args.length; | ||
|
||
for (var i = start; i < end; ++i) { | ||
ret[i - start] = args[i]; | ||
} | ||
|
||
return ret; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "sliced", | ||
"version": "0.0.0", | ||
"description": "A faster alternative to Array.prototype.slice.call.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/aheckmann/sliced" | ||
}, | ||
"keywords": [ | ||
"arguments", | ||
"slice", | ||
"array" | ||
], | ||
"author": "Aaron Heckmann <aaron.heckmann+github@gmail.com>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"mocha": "1.5.0", | ||
"benchmark": "~1.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
var sliced = require('../') | ||
var assert = require('assert') | ||
|
||
describe('sliced', function(){ | ||
it('exports a function', function(){ | ||
assert.equal('function', typeof sliced); | ||
}) | ||
describe('with 1 arg', function(){ | ||
it('returns an array of the arg', function(){ | ||
var o = [3, "4", {}]; | ||
var r = sliced(o); | ||
assert.equal(3, r.length); | ||
assert.equal(o[0], r[0]); | ||
assert.equal(o[1], r[1]); | ||
assert.equal(o[1], r[1]); | ||
}) | ||
}) | ||
describe('with 2 args', function(){ | ||
it('returns an array of the arg starting at the 2nd arg', function(){ | ||
var o = [3, "4", 5, null]; | ||
var r = sliced(o, 2); | ||
assert.equal(2, r.length); | ||
assert.equal(o[2], r[0]); | ||
assert.equal(o[3], r[1]); | ||
}) | ||
}) | ||
describe('with 3 args', function(){ | ||
it('returns an array of the arg from the 2nd to the 3rd arg', function(){ | ||
var o = [3, "4", 5, null]; | ||
var r = sliced(o, 1, 2); | ||
assert.equal(1, r.length); | ||
assert.equal(o[1], r[0]); | ||
}) | ||
}) | ||
}) |