What is sliced?
The 'sliced' npm package is a utility that provides a simple way to slice arrays and array-like objects. It is particularly useful for converting arguments objects into arrays and for slicing arrays in a concise manner.
What are sliced's main functionalities?
Slicing Arrays
This feature allows you to slice an array from a start index to an end index. In this example, the array [1, 2, 3, 4, 5] is sliced from index 1 to index 3, resulting in [2, 3].
const sliced = require('sliced');
const array = [1, 2, 3, 4, 5];
const result = sliced(array, 1, 3);
console.log(result); // Output: [2, 3]
Slicing Arguments Object
This feature is useful for converting the arguments object into an array and slicing it. In this example, the arguments object is sliced from index 1, resulting in [2, 3, 4].
const sliced = require('sliced');
function example() {
const args = sliced(arguments, 1);
console.log(args); // Output: [2, 3, 4]
}
example(1, 2, 3, 4);
Other packages similar to sliced
array-slice
The 'array-slice' package provides similar functionality to 'sliced' by allowing you to slice arrays and array-like objects. It is a lightweight alternative and can be used in the same scenarios where 'sliced' is used.
lodash
Lodash is a popular utility library that provides a wide range of functions for manipulating arrays, objects, and other data types. It includes a 'slice' function that offers similar functionality to 'sliced', but also provides many other utilities, making it a more comprehensive solution.
underscore
Underscore is another utility library that offers a variety of functions for working with arrays, objects, and other data types. It includes a 'rest' function that can be used to slice arrays and array-like objects, similar to 'sliced'.
#sliced
A faster alternative to [].slice.call(arguments)
.
Example output from benchmark.js
Array.prototype.slice.call x 1,320,205 ops/sec ±2.35% (92 runs sampled)
[].slice.call x 1,314,605 ops/sec ±1.60% (95 runs sampled)
cached slice.call x 10,468,380 ops/sec ±1.45% (95 runs sampled)
sliced x 16,608,237 ops/sec ±1.40% (92 runs sampled)
fastest is sliced
Array.prototype.slice.call(arguments, 1) x 1,383,584 ops/sec ±1.73% (97 runs sampled)
[].slice.call(arguments, 1) x 1,494,735 ops/sec ±1.33% (95 runs sampled)
cached slice.call(arguments, 1) x 10,085,270 ops/sec ±1.51% (97 runs sampled)
sliced(arguments, 1) x 16,620,480 ops/sec ±1.29% (95 runs sampled)
fastest is sliced(arguments, 1)
Array.prototype.slice.call(arguments, -1) x 1,303,262 ops/sec ±1.62% (94 runs sampled)
[].slice.call(arguments, -1) x 1,325,615 ops/sec ±1.36% (97 runs sampled)
cached slice.call(arguments, -1) x 9,673,603 ops/sec ±1.70% (96 runs sampled)
sliced(arguments, -1) x 16,384,575 ops/sec ±1.06% (91 runs sampled)
fastest is sliced(arguments, -1)
Array.prototype.slice.call(arguments, -2, -10) x 1,404,390 ops/sec ±1.61% (95 runs sampled)
[].slice.call(arguments, -2, -10) x 1,514,367 ops/sec ±1.21% (96 runs sampled)
cached slice.call(arguments, -2, -10) x 9,836,017 ops/sec ±1.21% (95 runs sampled)
sliced(arguments, -2, -10) x 18,544,882 ops/sec ±1.30% (91 runs sampled)
fastest is sliced(arguments, -2, -10)
Array.prototype.slice.call(arguments, -2, -1) x 1,458,604 ops/sec ±1.41% (97 runs sampled)
[].slice.call(arguments, -2, -1) x 1,536,547 ops/sec ±1.63% (99 runs sampled)
cached slice.call(arguments, -2, -1) x 10,060,633 ops/sec ±1.37% (96 runs sampled)
sliced(arguments, -2, -1) x 18,608,712 ops/sec ±1.08% (93 runs sampled)
fastest is sliced(arguments, -2, -1)
Benchmark source.
##Usage
sliced
accepts the same arguments as Array#slice
so you can easily swap it out.
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);
}
install
npm install sliced
LICENSE