Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread array performance (vs slice, splice, concat)
(version: 0)
Comparing performance of:
Splice vs Slice vs Concat vs Spread
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Splice
const testArray = [1, 2, 3]; const newTestArray = testArray.splice(0);
Slice
const testArray = [1, 2, 3]; const newTestArray = testArray.slice();
Concat
const testArray = [1, 2, 3]; const newTestArray = testArray.concat();
Spread
const testArray = [1, 2, 3]; const newTestArray = [...testArray];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Splice
Slice
Concat
Spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Splice
9741041.0 Ops/sec
Slice
21371486.0 Ops/sec
Concat
15229777.0 Ops/sec
Spread
10443658.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different approaches to manipulate arrays in JavaScript is an essential task for any developer. Let's break down the provided JSON data and explain what's being tested, the pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark definition provides a brief description of the test case: creating a new array by manipulating an existing one using `splice`, `slice`, `concat`, or spread syntax (`...`). **Individual Test Cases** There are four test cases: 1. **Splice**: Creates a new array by removing elements from the original array using `splice`. This approach modifies the original array and returns a new one. 2. **Slice**: Creates a new array by copying a subset of elements from the original array using `slice`. 3. **Concat**: Creates a new array by concatenating two or more arrays together using `concat`. 4. **Spread**: Creates a new array by spreading the elements of an existing array into a new one using spread syntax (`...`). **Pros and Cons** Here's a brief summary of each approach: * **Splice**: Pros: + Modifies the original array, which can be useful in some scenarios. + Can be faster than other approaches for large arrays. Cons: + Modifies the original array, which may not be desirable in all cases. + Can lead to unexpected side effects if not used carefully. * **Slice**: Pros: + Creates a new array without modifying the original one. + Can be safer and more predictable than `splice`. Cons: + May be slower for large arrays due to the overhead of creating a new array. * **Concat**: Pros: + Creates a new array without modifying the original ones. + Can be useful when combining multiple arrays together. Cons: + May lead to performance issues if dealing with very large arrays. + Returns a new array, which may not be desirable in some scenarios. * **Spread**: Pros: + Creates a new array without modifying the original one. + Can be safer and more predictable than `slice`. Cons: + Limited support for older browsers or environments. + May lead to unexpected behavior if used with certain data types. **Library Used** None of the test cases use any external libraries, so there's no library-specific consideration to discuss. **Special JavaScript Features/Syntax** There are no special JavaScript features or syntax used in these benchmarks. The code is straightforward and uses standard JavaScript constructs. **Other Alternatives** If you want to explore alternative approaches to manipulating arrays in JavaScript, consider the following: * **Array.prototype.map()**: Creates a new array by applying a transformation function to each element of the original array. * **Array.prototype.reduce()**: Creates a new array by accumulating values from an input array using a reduction function. * **Array.prototype.filter()**: Creates a new array by filtering elements from the original array based on a predicate function. These alternatives may have different performance characteristics or use cases, so it's essential to evaluate them in specific scenarios to determine which one is best suited for your needs.
Related benchmarks:
simple spread vs concat benchmark
Splice+Spread vs concat to concat arrays
unshift vs spread vs concat
Concat vs Spread for Large Arrayss
Splice vs Spread vs Unshift vs Concat to insert at beginning of array (fixed from slice)
Comments
Confirm delete:
Do you really want to delete benchmark?