Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 50df0d621faf860a9368c6a1f3fbf7b1911cee50 0 parents
@aheckmann authored
2  .gitignore
@@ -0,0 +1,2 @@
+*.sw*
+node_modules/
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
5 History.md
@@ -0,0 +1,5 @@
+
+0.0.1 / 2012-09-29
+===================
+
+
5 Makefile
@@ -0,0 +1,5 @@
+
+test:
+ @time ./node_modules/.bin/mocha $(T) $(TESTS)
+
+.PHONY: test
38 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 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  index.js
@@ -0,0 +1 @@
+module.exports = exports = require('./lib/sliced');
24 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 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 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]);
+ })
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.