Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.push vs array.map
(version: 0)
Comparing performance of:
array.push vs array.map
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var strs = Array.from(new Array(100000)).map(() => 'String concat. ') var result = []
Tests:
array.push
for (let i = 0; i < strs.length; i++) { result.push(`${strs[i]}_ok_${strs[i] * 532}`) }
array.map
result = strs.map((a) => `${strs[i]}_ok_${strs[i] * 532}`)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.push
array.map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0
Browser/OS:
Firefox 125 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array.push
89.1 Ops/sec
array.map
107.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Purpose** The benchmark measures the performance difference between using `Array.prototype.push()` versus `Array.prototype.map()` to concatenate strings in an array. **Options Compared** Two options are compared: 1. `array.push`: This method appends a new element to the end of the array and returns the length of the updated array. 2. `array.map()`: This method creates a new array with the results of applying a provided function to each element in the original array. **Pros and Cons** Here are some pros and cons for each approach: * `array.push`: + Pros: Simple, widely supported, and can be more efficient for small arrays. + Cons: Creates a new array object, which can lead to increased memory allocation and garbage collection overhead. Can also be slower due to the need to resize the array. * `array.map()`: + Pros: More concise and expressive than `push()`, especially when working with larger datasets. Returns an entirely new array, reducing side effects on the original data. + Cons: Creates a new array object, which can lead to increased memory allocation and garbage collection overhead. Can also be slower due to the need to create and copy the new array. **Library Usage** There is no specific library used in this benchmark. However, `Array.prototype.map()` uses the built-in JavaScript method for creating arrays with transformed data. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's required for basic arithmetic operations and string concatenation. **Alternatives** Other alternatives for array concatenation could include: * Using a loop with `push()` instead of `map()`: `for (let i = 0; i < strs.length; i++) { result.push(strs[i] + '_ok_' + strs[i] * 532) }` * Using other methods like `Array.prototype.reduce()`, which can be more efficient for large arrays. In conclusion, the benchmark provides a clear comparison between two common methods for array concatenation in JavaScript. Understanding the trade-offs and pros/cons of each approach can help developers make informed decisions about performance-critical code paths.
Related benchmarks:
Map Push vs Map return
Push vs Apply/Map
foreach push vs map
Push (forEach) vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?