Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array construct vs array push vs array concat
(version: 0)
Comparing performance of:
array construct vs array push vs array concat
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
array construct
let a = []; for(let i=0; i <= 1000; i=i+1) { a = [...a, i ]; };
array push
const a = []; for(let i=0; i <= 1000; i = i + 1) { a.push(i); };
array concat
let a = []; for(let i=0; i <= 1000; i = i + 1) { a = a.concat([i]); };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
array construct
array push
array concat
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array construct
3431.7 Ops/sec
array push
490038.9 Ops/sec
array concat
7859.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark compares three different approaches to construct an array in JavaScript: using `Array.prototype.push()`, `Array.prototype.concat()`, and creating a new array from scratch using spread syntax (`Array.prototype.slice()` is not used, but rather ` [...array, element]`). **Test Case Comparison** Each test case measures the performance of one specific approach: 1. **Array Construct**: This test case uses a traditional loop to create an array by pushing elements onto it. 2. **Array Push**: This test case uses the `push()` method to append elements to an existing array. 3. **Array Concat**: This test case uses the `concat()` method to merge arrays and add new elements. **Options Compared** The benchmark compares these three approaches, which have different performance characteristics: * **Array Construct**: Can be slower because it involves a loop and pushing elements onto the array, which can lead to memory reallocations. * **Array Push**: Typically faster than `Array Construct` because it only involves pushing elements onto an existing array, without the need for loop iterations or memory reallocations. * **Array Concat**: Often slower than both `Array Construct` and `Array Push`, as it creates a new array by merging existing arrays. **Pros and Cons of Each Approach** Here's a brief summary: * **Array Construct**: + Pros: Simple to implement, easy to understand. + Cons: Can be slow due to loop iterations and memory reallocations. * **Array Push**: + Pros: Generally fast, efficient use of existing array space. + Cons: Limited control over the resulting array structure. * **Array Concat**: + Pros: Flexible for merging arrays, but can lead to slower performance. + Cons: Can create a new array with additional memory usage. **Other Considerations** When choosing an approach: * Use `Array Construct` when simplicity and readability are more important than performance. * Choose `Array Push` when you need to append elements to an existing array frequently, as it tends to be faster. * Consider using `Array Concat` for merging arrays or adding large amounts of data, but be aware that it may lead to slower performance due to memory reallocations. **Library and Special Features** No specific libraries are mentioned in the benchmark definition. However, JavaScript has many other useful features and built-in methods that can affect array operations, such as: * `Array.prototype.reduce()` * `Array.prototype.forEach()` * `Array.prototype.map()` These methods can provide alternative ways to manipulate arrays, but may have performance implications depending on the specific use case. **Alternatives** Other alternatives for constructing or manipulating arrays in JavaScript include: * Using `Array.from()` to create an array from a iterable source * Utilizing libraries like Lodash (which provides functions for array manipulation) * Implementing custom solutions using various data structures and algorithms Keep in mind that the best approach often depends on specific requirements, performance constraints, and personal coding style.
Related benchmarks:
Array spread vs. push performance
Array push vs spread vs concat
Array#concat vs Array#push
spread vs concat vs unshift vs push
Comments
Confirm delete:
Do you really want to delete benchmark?