Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array = [] vs array.length = 0
(version: 0)
Comparing performance of:
array = [] vs array length = 0
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
Tests:
array = []
arr = [] arr.push(...['a', 'b', 'c']);
array length = 0
arr.length = 0; arr.push(...['a', 'b', 'c']);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array = []
array length = 0
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array = []
9938955.0 Ops/sec
array length = 0
7463551.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is tested in the provided JSON benchmark definition. **Benchmark Test Case:** The test case compares two ways to initialize an array: 1. **"array = []"`**: This method creates a new array using the assignment operator (`=`) and initializes it with an empty value (`[]`). 2. **"array.length = 0;" followed by "array.push(...['a', 'b', 'c'])"`**: This method first sets the length of the array to 0 (`arr.length = 0;`) and then uses the `push()` method to add elements to the array. **Comparison Options:** The benchmark tests both options against each other. The main difference between the two approaches is when the array is initialized: * In the first option, the array is created with an empty value (`[]`). * In the second option, the array is first initialized with a length of 0 and then populated with elements using `push()`. **Pros and Cons:** * **"array = []"`: + Pros: Creates the array in a single operation, which might be faster. + Cons: Requires more memory to create an empty array. * **"array.length = 0;" followed by "array.push(...['a', 'b', 'c'])"`: + Pros: Uses less memory since it doesn't create an empty array initially. + Cons: Creates the array incrementally, which might be slower. **Other Considerations:** The benchmark also considers the performance difference between these two approaches on a specific test case. In this case, both methods are used to populate the same array with 26 elements (`['a', 'b', 'c', ..., 'z']`). **Library and Special JS Features:** There is no library mentioned in the provided JSON benchmark definition. No special JavaScript features or syntax are used in these test cases. **Other Alternatives:** If you want to compare other array initialization methods, you could consider adding additional benchmark definitions, such as: * Using `Array.from()` to create an array * Using a loop to populate the array incrementally * Using `concat()` to concatenate arrays Keep in mind that each of these alternatives would require its own test case and may have different performance characteristics. Let me know if you'd like me to explain anything further or provide examples for other benchmark cases!
Related benchmarks:
JSON stringification versus simple map
Array.from vs Array spread with mapping of values2
`Array.slice(0, N)` vs `Array.length = N` sd434332432
arr.slice() vs [...arr]
Comments
Confirm delete:
Do you really want to delete benchmark?