Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Push Vs Spread for multiple conditional inserts
(version: 7)
Comparing performance of:
Push vs Spread
Created:
2 years ago
by:
Registered User
Jump to the latest result
Tests:
Push
const arr = [] if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } if(true === true) { arr.push({foo:"bar"}) } console.log(arr)
Spread
const arr = [ ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ...(true === true ? [{foo:"bar"}]:[]), ] console.log(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Push
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 what's being tested in this benchmark and compare the two approaches. **Benchmark Definition** The benchmark is defined as a simple JavaScript snippet that creates an array and pushes (or spreads) objects into it under different conditions. The objective is to measure which approach performs better: using `push` or `spread` with conditional statements. **Test Cases** There are two test cases: 1. **Push**: This test case uses the `push` method to add objects to an array, conditionally creating a new object and pushing it into the array. 2. **Spread**: This test case uses the spread operator (`...`) to create a new array with conditional statements, adding objects to the array. **Comparison** Both approaches have their pros and cons: * **Push**: + Pros: Simple and straightforward implementation. + Cons: May lead to unnecessary object creations, potentially impacting performance for large arrays or complex conditions. + Additional considerations: When using `push`, it's essential to consider the impact of creating new objects on memory allocation and garbage collection. * **Spread**: + Pros: More concise and expressive syntax can reduce the number of necessary object creations. + Cons: May be slower due to the overhead of evaluating conditional statements and array operations. + Additional considerations: When using `spread`, consider the impact of creating new arrays and objects on memory allocation and garbage collection. **Library usage** Neither of these test cases uses any external libraries. However, it's worth noting that in a real-world scenario, you might need to use libraries like Lodash or Ramda for more complex array operations or conditional statements. **Special JS features** There are no special JavaScript features or syntax used in this benchmark (e.g., async/await, generators, etc.). **Other alternatives** If you wanted to test other approaches, here are a few options: * **Array.prototype.unshift**: Instead of using `push`, you could use `unshift` to add objects to the beginning of the array. * **Array.prototype.splice**: You could use `splice` to insert or remove elements from an existing array. * **Array.from()**: Another approach would be to use `Array.from()` to create a new array with a specified iterable (e.g., an array of conditional statements). **Benchmark preparation code** Since the benchmark definition does not include any specific preparation code, the benchmark assumes that the test environment is already set up and ready for execution. The test cases only contain the JavaScript snippets to be executed. The latest benchmark results show that Chrome 118 performs better with `Push` (103322 executions per second) compared to `Spread` (98017 executions per second). However, it's essential to consider the actual use case and requirements when choosing between these approaches.
Related benchmarks:
array update push vs spread
Javascript: Spread vs push
JS array spread operator vs push
Splice vs Spread vs Unshift vs Push to insert at beginning of array
Spread vs Push when adding into array
Comments
Confirm delete:
Do you really want to delete benchmark?