Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Shallow Copy Array - Slice vs Spread vs Filter
(version: 0)
Comparing performance of:
Slice vs Spread vs Filter
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Slice
array = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]; arrayCopy = array.slice();
Spread
array = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]; arrayCopy = [...array];
Filter
array = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]; arrayCopy = array.filter(() => true);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Slice
Spread
Filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Slice
3982710.5 Ops/sec
Spread
3902864.2 Ops/sec
Filter
3220043.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explore what is being tested, the options compared, pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark is designed to test three different approaches for creating a shallow copy of an array: using `slice()`, `spread` operator (`...`), and `filter()`. **Options Compared** 1. **Slice**: Creates a new array by extracting a portion of the original array using the `slice()` method. 2. **Spread Operator**: Creates a new array by spreading the elements of the original array into a new array using the spread operator (`...`). 3. **Filter**: Creates an empty array by filtering out all elements from the original array, returning only true values (which are considered as empty values). **Pros and Cons of Each Approach** 1. **Slice** * Pros: Fast and efficient way to create a shallow copy of an array. * Cons: Can be brittle if the number of elements is large, as it creates a new array with references to the original elements. 2. **Spread Operator** * Pros: Creates a new array without modifying the original, which can be safer for very large arrays. * Cons: Requires JavaScript 2015+ syntax, and may not be as performant as `slice()` on smaller arrays. 3. **Filter** * Pros: Can create an empty array with no overhead if all elements need to be filtered out. * Cons: May be slower than `slice()` or spread operator for most use cases. **Library/Function Usage** None of the test cases uses any external libraries or functions beyond the standard JavaScript `Array` methods. The tests rely on built-in JavaScript features and APIs. **Special JS Features/Syntax** The benchmark does not explicitly test any special JavaScript features or syntax, such as async/await, ES6 classes, or modern ECMAScript modules. It only uses standard JavaScript features for array manipulation and comparison. **Other Considerations** 1. **Array Length**: The original array has 40 elements, which may affect the performance differences between the approaches. 2. **Browser Variability**: The benchmark results are from Chrome 120 on a Windows desktop, which might not be representative of other browsers or platforms. 3. **Execution Frequency**: The number of executions per second (e.g., 3982710.5 for the "Slice" test) can impact performance differences between the approaches. **Alternatives** Other approaches to create shallow copies of arrays in JavaScript include: 1. `Array.prototype.concat()`: Creates a new array by concatenating multiple arrays. 2. `Object.assign()`: Creates a new object by copying properties from an existing object. 3. `Array.prototype.map()` or `Array.prototype.forEach()`: Can be used to create new arrays with transformed data. These alternatives may have their own trade-offs and performance characteristics, depending on the specific use case and requirements.
Related benchmarks:
Array slice vs array filter
JavaScript array copy via spread op vs slice
Slice vs spread array
Array.slice() vs. Spread operator (10000 items)
Comments
Confirm delete:
Do you really want to delete benchmark?