Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Concat vs push(...) for large arrays 2
(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(element, 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):
Let's break down the benchmark test case and explain what is being tested. **Benchmark Test Case** The test case is comparing three different approaches to append to a large array: `push()`, `concat()`, and `spread()` methods. **Options Being Compared** * **`control.push.apply(element, window.test)`**: This approach uses the `apply()` method to apply the `push()` method to an array. The `element` parameter is not used in this test case. * **`window.top.tests.concat.push(window.test)`**: This approach uses the `push()` method on an array created using the `concat()` method. In other words, a new array is created and then pushed with the original array. * **`[...window.test].push(...)`**: This approach uses the spread operator (`...`) to create a new array from the original array, and then pushes elements onto it. **Pros and Cons of Each Approach** * **`control.push.apply(element, window.test)`**: + Pros: Efficient in terms of memory allocation, as it modifies an existing array. + Cons: May be slower due to the use of `apply()`, which can introduce additional overhead. * **`window.top.tests.concat.push(window.test)`**: + Pros: Can be faster than the first approach, as it creates a new array and then pushes elements onto it. + Cons: Allocates more memory, as it creates a new array. * **`[...window.test].push(...)`**: + Pros: Most efficient in terms of memory allocation, as it creates a new array without modifying an existing one. + Cons: May be slower due to the creation of a new array. **Library and Purpose** None are explicitly mentioned in this test case. However, `apply()` is a built-in JavaScript method that allows you to apply a function to a specific context or scope. **Special JS Feature or Syntax** The spread operator (`...`), which is used in the third approach, was introduced in ECMAScript 2015 (ES6) as a way to create new arrays from existing ones. **Other Considerations** * **Memory Allocation**: The test case highlights the importance of memory allocation when working with large arrays. Approaches that allocate more memory may have performance implications. * **Cache Locality**: The use of `push()` on an array can improve cache locality, as it modifies a contiguous block of memory. **Alternatives** Other approaches to append to an array include: * Using `Array.prototype.push()```` window.top.tests.control = window.top.tests.control.concat([window.test]); ``` This approach uses the `concat()` method to create a new array and then pushes elements onto it, similar to the second approach in the test case.
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?