Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Concat vs push(...) for large arrays 3
(version: 0)
Comparing the various ways to append to a large array
Comparing performance of:
Control (for push) vs Concat vs Spread
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.top.tests = {control:[], concat:[], spread:[]}; window.test = (new Array(10)).fill(null); window.cutoff = 5000;
Tests:
Control (for push)
window.top.tests.control.push.apply(window.top.tests.control, window.test); if (window.top.tests.control.length > window.cutoff) { window.top.tests.control = []; console.log('reset control'); }
Concat
window.top.tests.concat = window.top.tests.concat.concat(window.test); if (window.top.tests.concat.length > window.cutoff) { window.top.tests.concat = []; console.log('reset concat'); }
Spread
window.top.tests.spread.push(...window.test); if (window.top.tests.spread.length > window.cutoff) { window.top.tests.spread = []; console.log('reset spread'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Control (for push)
Concat
Spread
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):
Measuring the performance of different approaches to append elements to an array in JavaScript can be interesting. **Benchmark Goal** The goal is to compare three methods: 1. **`push()`**: Using the `push()` method to add one or more elements to the end of an array. 2. **Concatenation (`concat()`)**: Using the `concat()` method to create a new array by concatenating another array with the existing one. 3. **Spread operator (`...`)**: Using the spread operator (`...` ) to expand an array into its elements. **Options Compared** These three methods are compared in terms of their performance on large arrays. **Pros and Cons** * `push()`: This is a simple and efficient method that adds elements to the end of an array. However, it can be slow if the array is very large because JavaScript needs to update the internal array length and shift all the elements down. * Concatenation (`concat()`): Creating a new array using `concat()` requires additional memory allocation and copying the data from one array to another, which can be slower than `push()`. * Spread operator (`...`): This is generally faster than concatenation because it avoids creating a temporary array. However, it may require more memory if the spread operator needs to copy all elements of the input array. **Library/Functionality Used** None are mentioned in this benchmark definition. **Special JS Features/Syntax** The only special syntax used here is the spread operator (`...`), which was introduced in ECMAScript 2015 (ES6). **Other Alternatives** There may be other methods to append elements to an array, such as: * `unshift()` instead of `push()`: This method adds one or more elements to the beginning of an array. * Using a `for` loop with indexing: Instead of using array methods like `push()` or spread operator, you could use a `for` loop with indexing to iterate over the array and add elements. These alternatives might be worth considering if performance is critical, but they may have different trade-offs in terms of code readability and maintainability.
Related benchmarks:
Concat vs push(...) vs spread for large arrays
Concat vs push(...) vs. spread for large arrays
Concat vs push(...) for large arrays using spread in test 2
Concat vs push(...) vs push.apply for large arrays
Concat vs push(...) for large arrays with PrototypePushApply
Comments
Confirm delete:
Do you really want to delete benchmark?