Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
clearing array via pop() vs shift() vs .length = 0 vs = [] (200k)
(version: 0)
Comparing performance of:
pop vs shift vs length vs reassignment vs .splice(0)
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for(var i = 0; i < 200000; i++){array.push(Math.random());}
Tests:
pop
while (array.length > 0) { array.pop(); }
shift
while (array.length > 0) { array.shift(); }
length
array.length = 0
reassignment
array = []
.splice(0)
array.splice(0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
pop
shift
length
reassignment
.splice(0)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
pop
102429048.0 Ops/sec
shift
512662112.0 Ops/sec
length
25776030.0 Ops/sec
reassignment
48016672.0 Ops/sec
.splice(0)
16268123.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different JavaScript array manipulation techniques is crucial for understanding how to optimize code for efficient execution. **Benchmark Overview** The benchmark measures the time taken by four methods to clear an array with 200,000 elements: 1. Using `pop()` repeatedly until the array is empty 2. Using `shift()` repeatedly until the array is empty 3. Setting `array.length` to 0 4. Reassigning the array to an empty array (`[]`) 5. Using `.splice(0)` (which removes the first element from the array and returns it) **Options Compared** The benchmark compares the performance of these four methods: 1. **Pop()**: Removes elements from the end of the array until it's empty. * Pros: Can be useful when removing elements in a specific order. * Cons: May be slower than other methods, especially for large arrays. 2. **Shift()**: Removes elements from the beginning of the array until it's empty. * Pros: Can be faster than `pop()` for large arrays since it only needs to access the first element. * Cons: May not be suitable when removing elements in a specific order. 3. **Length** (or `array.length = 0`): Sets the length of the array to 0, which effectively clears it. * Pros: Simple and efficient way to clear an array. * Cons: Can be slower than other methods since it needs to update the array's internal length property. 4. **Reassignment** (or `array = []`): Reassigns the entire array to a new empty array. * Pros: Most straightforward way to clear an array, but also potentially slow due to memory reallocation. * Cons: Can lead to performance issues if done repeatedly. **Library and Special JS Features** No notable libraries or special JavaScript features are used in this benchmark. The focus is solely on the performance of the four array manipulation techniques. **Other Alternatives** Some other methods for clearing an array include: 1. `array.length = 0` with a loop: `while (array.length > 0) { array.pop(); }` 2. `for (var i = array.length; i--; ) { array.splice(i, 1); }` While these alternatives might have their own performance characteristics, they are not included in the benchmark. **Key Takeaways** * For large arrays, using `shift()` or reassigning the array to an empty array (`[]`) can be faster than other methods. * Setting `array.length` to 0 is a simple and efficient way to clear an array, but might incur some overhead due to internal property updates. * Using `pop()` repeatedly until the array is empty can be slower than other methods for large arrays. This benchmark provides valuable insights into the performance characteristics of different JavaScript array manipulation techniques, which can help developers optimize their code for better execution.
Related benchmarks:
clearing array via pop() vs shift() vs .length = 0 vs = []
clearing array via pop() vs shift() vs .length = 0 vs = []1
clearing array via pop() vs shift() vs .length = 0 vs = [] 2322
Empty an array in JavaScript
Comments
Confirm delete:
Do you really want to delete benchmark?