Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
empty an array in JavaScript and then reassign
(version: 0)
Based on question on http://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript
Comparing performance of:
Just instantiate new array vs Set length to zero
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Just instantiate new array
var size = 10000; var arr1 = []; for (var i = 0; i < size; i++){ arr1.push(i); } arr1 = [1,2,3,4,5];
Set length to zero
var size = 10000; var arr2 = []; for (var i = 0; i < size; i++){ arr2.push(i); } arr2.length = 0; arr2.push(1,2,3,4,5)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Just instantiate new array
Set length to zero
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Just instantiate new array
37862.5 Ops/sec
Set length to zero
38041.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is designed to measure how quickly JavaScript arrays can be emptied and reassigned in two different ways: 1. **Just instantiate new array**: Create a new array with 10,000 elements using `arr1.push(i)` and then immediately create another new array `arr2` and do the same. 2. **Set length to zero**: Create an array with 10,000 elements using `arr2.push(i)`, set its length to 0 using `arr2.length = 0`, and then add new elements to it. **Library and Special JS Features** In both benchmark definitions, there is no explicit mention of a library or special JavaScript features. However, it's worth noting that `push()` method is used to append elements to an array. The use of `var` keyword for variable declarations might also be considered a legacy feature in some contexts. **Comparison of Approaches** The two approaches differ in their performance: 1. **Just instantiate new array**: This approach creates two separate arrays, which might lead to additional memory allocation and garbage collection overhead. However, it avoids any modifications to the original array. 2. **Set length to zero**: This approach modifies the original array by setting its length to 0, which can be more efficient than creating a new array. However, this approach requires more operations (pushing elements) on the modified array. **Pros and Cons** 1. **Just instantiate new array**: * Pros: avoids modifying the original array, potentially fewer operations. * Cons: creates two separate arrays, may lead to additional memory allocation and garbage collection overhead. 2. **Set length to zero**: * Pros: more efficient than creating a new array, can be faster for larger datasets. * Cons: modifies the original array, requires more push() operations. **Other Alternatives** If you want to test other approaches or variations on these two methods, some alternatives could include: 1. **Using `splice()` instead of `push()`**: Instead of using `push()` to add elements, you could use `splice()` to remove elements from the end of the array. 2. **Using `map()` instead of a loop**: You could test if using `map()` or other array methods can improve performance compared to the original benchmark. 3. **Testing with larger datasets**: Increasing the size of the dataset (e.g., 100,000 elements) might help identify performance differences between the two approaches. Keep in mind that the actual performance difference between these approaches may depend on various factors, such as the specific JavaScript engine, browser, and hardware used to run the benchmark.
Related benchmarks:
empty an array in JavaScript?
empty an array in JavaScript?
teststest
teststest1
empty an array in JavaScript - splice vs setting length faster
Comments
Confirm delete:
Do you really want to delete benchmark?