From 50df0d621faf860a9368c6a1f3fbf7b1911cee50 Mon Sep 17 00:00:00 2001 From: Aaron Heckmann Date: Sat, 29 Sep 2012 00:09:09 -0700 Subject: [PATCH] initial commit --- .gitignore | 2 ++ .travis.yml | 4 ++++ History.md | 5 +++++ Makefile | 5 +++++ README.md | 38 ++++++++++++++++++++++++++++++++++++++ bench.js | 31 +++++++++++++++++++++++++++++++ index.js | 1 + lib/sliced.js | 24 ++++++++++++++++++++++++ package.json | 24 ++++++++++++++++++++++++ test/index.js | 36 ++++++++++++++++++++++++++++++++++++ 10 files changed, 170 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 History.md create mode 100644 Makefile create mode 100644 README.md create mode 100644 bench.js create mode 100644 index.js create mode 100644 lib/sliced.js create mode 100644 package.json create mode 100644 test/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..be106ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.sw* +node_modules/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..09c230f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - 0.6 + - 0.8 diff --git a/History.md b/History.md new file mode 100644 index 0000000..929682b --- /dev/null +++ b/History.md @@ -0,0 +1,5 @@ + +0.0.1 / 2012-09-29 +=================== + + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3dd8a2d --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ + +test: + @time ./node_modules/.bin/mocha $(T) $(TESTS) + +.PHONY: test diff --git a/README.md b/README.md new file mode 100644 index 0000000..e1e3f8d --- /dev/null +++ b/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) diff --git a/bench.js b/bench.js new file mode 100644 index 0000000..3be0747 --- /dev/null +++ b/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 + * + */ diff --git a/index.js b/index.js new file mode 100644 index 0000000..3b90ae7 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = exports = require('./lib/sliced'); diff --git a/lib/sliced.js b/lib/sliced.js new file mode 100644 index 0000000..e716a1a --- /dev/null +++ b/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; +} + diff --git a/package.json b/package.json new file mode 100644 index 0000000..918bfd2 --- /dev/null +++ b/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 ", + "license": "MIT", + "devDependencies": { + "mocha": "1.5.0", + "benchmark": "~1.0.0" + } +} diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..2d5bdf1 --- /dev/null +++ b/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]); + }) + }) +})