Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread array performance (vs slice, splice, concat, for)
(version: 0)
Comparing performance of:
Splice vs Slice vs Concat vs Spread vs For cycle with preallocation
Created:
3 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];
For cycle with preallocation
const testArray = [1, 2, 3]; const testArrLen = testArray.length; const resArr = new Array(testArrLen); for (let i = 0; i < testArrLen; i++) { resArr[i] = testArray[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Splice
Slice
Concat
Spread
For cycle with preallocation
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript benchmarking test, specifically designed to compare the performance of different methods for creating a new array from an existing one. **Test Cases** There are four individual test cases: 1. **Splice**: Uses the `splice()` method to create a new array by removing the first element(s) of the original array. 2. **Slice**: Uses the `slice()` method to create a shallow copy of the original array, starting from index 0 and ending at index `-1`. 3. **Concat**: Uses the `concat()` method to concatenate an empty array with the original array, effectively creating a new array with all elements. 4. **Spread**: Uses the spread operator (`...`) to create a new array by spreading the elements of the original array. **Library and Special Features** None of the test cases rely on external libraries or special JavaScript features beyond what is built-in to the language. The `Array.prototype` methods (e.g., `splice()`, `slice()`, `concat()`) are all standard JavaScript methods. **Options Compared** The test compares four different methods: 1. **Splice**: Removes elements from the beginning of the array. 2. **Slice**: Creates a shallow copy of the original array, starting from index 0 and ending at index `-1`. 3. **Concat**: Concatenates an empty array with the original array. 4. **Spread**: Uses the spread operator to create a new array by spreading the elements of the original array. **Pros and Cons** Each method has its own trade-offs: * **Splice**: Creates a new array with the removed elements, modifying the original array. Can be efficient for small arrays but slower for large ones. * **Slice**: Creates a shallow copy of the original array, starting from index 0 and ending at index `-1`. Can be faster than `splice()` for smaller arrays but slower for larger ones. * **Concat**: Concatenates an empty array with the original array, creating a new array. Can be slower than other methods due to the overhead of concatenation. * **Spread**: Creates a new array by spreading the elements of the original array. Generally faster and more memory-efficient than `concat()`. **For Cycle with Preallocation** The fourth test case uses a for loop to preallocate an array with the same length as the original array, then copies elements from the original array into the newly allocated array. This method can be slower due to the overhead of dynamic array allocation but can also be faster for very large arrays. **Other Alternatives** Alternative methods that could potentially outperform or match these approaches include: * Using `Array.from()` instead of `spread()` * Implementing a custom array clone function * Using a library like Lodash's `cloneDeep()` function However, the performance differences between these methods are usually negligible unless working with very large datasets. **Benchmark Interpretation** The provided benchmark results show the relative performance of each method across different browsers and devices. The `ExecutionsPerSecond` value represents how many iterations (or operations) can be performed per second by the JavaScript engine. Lower values typically indicate better performance.
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?