Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
clearing array via pop() vs shift() vs .length = 0 vs = []
(version: 0)
Comparing performance of:
pop vs shift vs length vs reassignment
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for(var i = 0; i < 100000; 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 = []
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
pop
shift
length
reassignment
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
57825260.0 Ops/sec
shift
487956576.0 Ops/sec
length
24998056.0 Ops/sec
reassignment
47053520.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark data and explain what is tested, compared options, pros and cons, library usage, special JS features or syntax, and alternative approaches. **Benchmark Definition** The benchmark measures how fast four different methods are to clear an array with 100,000 elements. The arrays are populated with random numbers using `Math.random()` in the script preparation code. **Comparison Options** 1. **`pop()```**: Removes and returns the last element of the array. 2. **`shift()```: Removes and returns the first element of the array. 3. **Setting `.length = 0```**: Sets the length of the array to 0, effectively clearing it. 4. **Reassigning `array = []``**: Reassigns the array to a new empty array. **Pros and Cons** * **`pop()```**: * Pros: Efficient for large arrays since only one element needs to be removed from the end. * Cons: Can lead to memory fragmentation if not implemented carefully, as it involves shifting elements after removing the last one. This can result in inefficient use of memory. * **`shift()``**: * Pros: Similar to `pop()` but starts with removing the first element instead of the last. It's efficient for large arrays and doesn't lead to memory fragmentation like `pop()` does when done sequentially. * Cons: If implemented naively, it can be slower than using `.length = 0` for very large numbers of iterations because of potential overflows or cache behavior in JavaScript engines. * **`.length = 0```**: * Pros: Simple and efficient since it only involves updating the array's length information. It doesn't depend on any specific element being removed, making it more likely to be implemented quickly by a JavaScript engine. * Cons: May lead to an unnecessary allocation for each call if used in a loop (as seen with `for` loops), as the old elements are kept intact until they're explicitly deleted. However, this is more of a micro-optimization and generally less significant than its simplicity and speed advantages. * **`array = []```**: * Pros: Can be particularly efficient if used at the end of a loop where it removes all previously added elements in one step without any need for subsequent deletion operations, especially when combined with `.length = 0` or another form of final array clearing. * Cons: It requires reassigning the entire `array`, which can be slower than other methods since JavaScript has to perform a new allocation and copy operation. **Library Usage** There are no external libraries used in this benchmark. All tests rely on standard JavaScript operations provided by the engine or its libraries directly. **Special JS Features or Syntax** The only special feature is the use of `for...of` loop, which is a more modern way to iterate over arrays compared to traditional `for` loops with indices (`var i = 0; i < array.length; i++`). This feature was introduced in ECMAScript 2015 (ES6). Although not explicitly mentioned in the JSON data for individual test cases, it's worth noting that the script preparation code initializes an array and then populates it with random numbers using this modern loop.
Related benchmarks:
clearing array via pop() vs shift() vs .length = 0 vs = []1
clearing array via pop() vs shift() vs .length = 0 vs = [] 2322
clearing array via pop() vs shift() vs .length = 0 vs = [] (200k)
Empty an array in JavaScript
Comments
Confirm delete:
Do you really want to delete benchmark?