Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.length = 0 and push vs [...data]
(version: 0)
Comparing performance of:
array length = 0 and push vs array = [...data]
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [1,2,3,4]; var data = arr.slice()
Tests:
array length = 0 and push
arr.length = 0 arr.push(...data)
array = [...data]
arr = [...data]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array length = 0 and push
array = [...data]
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0.1 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.0.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array length = 0 and push
6596531.5 Ops/sec
array = [...data]
10729565.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark definition and explain what is being tested. **Benchmark Definition:** The benchmark is testing two different approaches to create an array in JavaScript: 1. `arr.length = 0` followed by `arr.push(...data)`: This approach creates an empty array, sets its length to 0, and then uses the spread operator (`...`) to add elements from the original array `data`. 2. `arr = [...data]`: This approach uses the spread operator (`...`) to create a new array that is a copy of the original array `data`. **Pros and Cons:** * **Approach 1 (push method)**: + Pros: Can be faster for large arrays, as it only creates a reference to the existing array. + Cons: Requires two operations: setting the length to 0 and pushing elements, which can be slower than creating an array directly with the spread operator. * **Approach 2 (spread operator)**: + Pros: Creates an array directly from the original data, which can be faster for small to medium-sized arrays. It also avoids the overhead of setting the length of an empty array. + Cons: Requires a new array object to be created, which can consume more memory. **Library and Purpose (if applicable):** There is no explicit library mentioned in the benchmark definition. However, it's worth noting that both approaches rely on the spread operator (`...`), which is a built-in feature of JavaScript. **Special JS Feature or Syntax:** The benchmark uses the spread operator (`...`) to create arrays. This syntax was introduced in ECMAScript 2015 (ES6) and has since become a standard part of JavaScript. **Other Alternatives:** * Instead of using the spread operator, you could use `Array.prototype.concat()` or `Array.prototype.slice()` to create a new array from the original data. * You could also use `Array.from()` to create an array from an iterable (like an array or string). The benchmark is designed to compare the performance of these two approaches on modern JavaScript engines. The test cases are likely intended to be representative of real-world scenarios where arrays need to be created and populated with data. In this specific benchmark, the results show that Approach 2 (spread operator) outperforms Approach 1 (push method) by a significant margin, especially for smaller arrays. However, for larger arrays, the push method may become faster due to its lower overhead.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]`
Test slice vs array.length accessing last element
`array.slice(-1)[0]` vs `array[array.length - 1]`
slice vs new array
Comments
Confirm delete:
Do you really want to delete benchmark?