Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
clearing array via pop() vs shift() vs .length = 0 vs = [] 2322
(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 < 1000000; 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the explanation. **Benchmark Definition** The benchmark tests four different ways to clear an array in JavaScript: 1. **pop()**: using the `pop()` method, which removes and returns the last element from the array, until the array is empty. 2. **shift()**: using the `shift()` method, which removes and returns the first element from the array, until the array is empty. 3. **length = 0**: setting the array's length property to 0 directly. 4. **reassignment**: assigning a new, empty array to the variable (`array = []`). 5. **splice(0)**: using the `splice()` method with an index of 0, which effectively removes all elements from the array. **Library and special JS features** None are used in this benchmark. **Options compared** The benchmark compares the execution speed (measured in executions per second) of each method to clear an array. The results show that: * `shift()` is the fastest method, with an average execution speed of 134721888.0 executions per second. * `pop()` comes next, with an average execution speed of 801611904.0 executions per second (note: this is actually slower than the other methods). * Setting `length = 0` has a moderate execution speed of 43147208.0 executions per second. * Reassigning `array = []` is significantly slower, with an average execution speed of 18003854.0 executions per second. * Using `splice(0)` is the slowest method, with an average execution speed of 47061336.0 executions per second. **Pros and cons** Here are some pros and cons for each method: * **pop()**: Pros - simple to implement; Cons - slower than other methods, modifies array in reverse order. * **shift()**: Pros - fast; Cons - modifies array in a way that might not be desirable (removes the first element). * `length = 0`: Pros - simplest and most efficient way to clear an array; Cons - may have performance issues if used with large arrays. * `reassignment`: Pros - no overhead; Cons - can lead to unexpected behavior if not handled properly. * **splice(0)**: Pros - easy to implement; Cons - slow, may have issues with large arrays. **Other considerations** When choosing a method to clear an array, consider the following: * If you need to maintain the original order of elements, use `pop()` or `shift()`. * If performance is critical and you're working with small arrays, use `length = 0` or reassignment. * Avoid using `splice(0)` unless you have a specific reason for doing so. **Alternatives** If you need to clear an array frequently, consider these alternatives: * Use a more efficient data structure, such as a linked list or a map. * Implement a custom clearing method that suits your specific needs. * Consider using a library like Lodash or Underscore.js, which provides optimized functions for common operations.
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 = [] 1
clearing array via pop() vs shift() vs .length = 0 vs = [] (200k)
Comments
Confirm delete:
Do you really want to delete benchmark?