Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Concat vs push(...) for large arrays using spread in test 2
(version: 0)
Comparing the various ways to append to a large array
Comparing performance of:
Control (for push) vs Concat vs Spread
Created:
2 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)
for (let element of window.test) window.top.tests.control.push(element); 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 = [...window.top.tests.spread, ...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):
**Overview** The provided benchmark, hosted on MeasureThat.net, compares three approaches for appending elements to a large array in JavaScript: `push()`, `concat()`, and the spread operator (`...`). The test aims to determine which approach is faster and more efficient. **Options Compared** 1. **`push()`**: This method modifies the array by adding one or more elements to the end of it. 2. **`concat()`**: This method returns a new array that contains all elements from the original array, followed by the elements passed as an argument (in this case, `window.test`). 3. **Spread Operator (`...`)**: This operator creates a new array with the elements of the original array and the elements passed as an argument. **Pros and Cons** 1. **`push()`**: * Pros: Fastest and most efficient way to append elements to an array. * Cons: Modifies the original array, which might be undesirable in some cases. 2. **`concat()`**: * Pros: Returns a new array, preserving the original array's integrity. * Cons: Slower than `push()`, as it creates a new array and copies all elements from the original array. 3. **Spread Operator (`...`)**: * Pros: Creates a new array without modifying the original array. * Cons: Can be slower than `push()` due to the overhead of creating a new array. **Library Usage** None of the test cases use external libraries, which is good for isolating the JavaScript engine's behavior and reducing potential interference from library-side optimizations or invariants. **Special JS Features/Syntax** The test does not utilize any special JavaScript features or syntax beyond what is necessary to demonstrate the three approaches. If additional features were used, it would require more context about their implementation and how they affect performance. **Alternative Approaches** 1. **Array.prototype.reduce()**: This method reduces an array by applying a callback function to each element, accumulating a result. While not directly comparable to `push()` or `concat()`, it can be used as an alternative way to append elements to an array. 2. **Array.prototype.map()**: Similar to `reduce()`, this method creates a new array with the results of applying a provided function to each element in the original array. Again, not directly comparable to `push()` or `concat()`. 3. **Native WebAssembly (WASM)**: If you need extreme performance, you could consider using native WASM code, which can be compiled to run on the JavaScript engine's optimized backend. However, this would require more complex setup and might not be necessary for most use cases. Keep in mind that these alternative approaches have different use cases and performance characteristics compared to `push()`, `concat()`, and the spread operator.
Related benchmarks:
Concat vs push(...) vs spread for large arrays
Concat vs push(...) vs. spread for large arrays
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?