Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array push vs array set
(version: 0)
Comparing performance of:
push vs array set vs array set 2
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = [...new Array(1000).keys()]
Tests:
push
var brray = [] brray.push(...array)
array set
var crray = [] for (let i = 0; i < array.length; i++) { crray[i] = array[i] }
array set 2
var drray = [] for (const [key, value] of array.entries()) { drray[key] = value }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
push
array set
array set 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
push
570787.1 Ops/sec
array set
15420.4 Ops/sec
array set 2
190138.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches for pushing elements onto an array or setting its values: `array.push()`, `for` loop, and destructuring assignment (`...` operator). **Script Preparation Code** The script preparation code creates an array with 1000 elements using `[...new Array(1000).keys()]`. This is a common pattern in JavaScript to create an array of numbers from 0 to n-1. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark only runs on the client-side (i.e., in the browser). **Individual Test Cases** There are three test cases: 1. `push`: This test case uses the `array.push()` method to add elements to an array. 2. `array set 2`: This test case uses a `for` loop with destructuring assignment (`...`) to add elements to an array. 3. `array set`: This test case uses a traditional `for` loop without destructuring assignment to add elements to an array. **Library and Special JS Features** None of the benchmark's code relies on any external libraries or special JavaScript features beyond what is required for standard JavaScript functionality (e.g., arrays, loops). **Comparison of Approaches** The three approaches being compared are: 1. **`array.push()`**: This method adds one or more elements to an array and returns the new length of the array. * Pros: + Easy to use and understand + Works for small to medium-sized arrays * Cons: + Has to traverse the entire array to add an element, which can lead to performance issues with large arrays 2. **`for` loop with destructuring assignment (`...`)**: This approach adds elements to an array using a `for` loop and destructuring assignment. * Pros: + Efficient for adding multiple elements to an array + Can be faster than `array.push()` for large arrays 3. **Traditional `for` loop without destructuring assignment**: This approach uses a traditional `for` loop with array indexing to add elements to an array. * Pros: + Simple and easy to understand * Cons: + Has to traverse the entire array to add an element, which can lead to performance issues with large arrays **Other Alternatives** In addition to these three approaches, other methods could be used to push elements onto an array or set its values, such as: * `array.splice()`: Adds one or more elements to an array and returns the new length of the array. * `array.forEach()` and `push`: This approach uses a `for...of` loop with `forEach()` to iterate over an array and push each element onto it. * Other libraries or specialized functions (e.g., Lodash) that provide optimized implementations for these operations. Overall, this benchmark aims to help users understand the trade-offs between different approaches for adding elements to an array, which can be important in performance-critical code.
Related benchmarks:
Push to array, vs ES6 Spread.
Array Push vs. Index Access
array update push vs spread
Array.from() vs new Array() vs push
Array.from() vs new Array() vs push pushup
Comments
Confirm delete:
Do you really want to delete benchmark?