Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aheckmann committed Sep 29, 2012
0 parents commit 50df0d6
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.sw*
node_modules/
4 changes: 4 additions & 0 deletions .travis.yml
@@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.6
- 0.8
5 changes: 5 additions & 0 deletions History.md
@@ -0,0 +1,5 @@

0.0.1 / 2012-09-29
===================


5 changes: 5 additions & 0 deletions Makefile
@@ -0,0 +1,5 @@

test:
@time ./node_modules/.bin/mocha $(T) $(TESTS)

.PHONY: test
38 changes: 38 additions & 0 deletions README.md
@@ -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)
31 changes: 31 additions & 0 deletions bench.js
@@ -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
*
*/
1 change: 1 addition & 0 deletions index.js
@@ -0,0 +1 @@
module.exports = exports = require('./lib/sliced');
24 changes: 24 additions & 0 deletions lib/sliced.js
@@ -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;
}

24 changes: 24 additions & 0 deletions package.json
@@ -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"
}
}
36 changes: 36 additions & 0 deletions test/index.js
@@ -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]);
})
})
})

0 comments on commit 50df0d6

Please sign in to comment.