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 vs .splice(0)
Created:
8 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 = []
.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:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
pop
93580616.0 Ops/sec
shift
92874864.0 Ops/sec
length
25220498.0 Ops/sec
reassignment
8925873.0 Ops/sec
.splice(0)
36727884.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Definition** The benchmark is designed to test different ways to clear an array in JavaScript. The script preparation code creates an empty array with 100,000 elements, fills it with random numbers using `Math.random()`, and then tests various methods to remove all elements from the array. **Test Cases** There are five test cases: 1. **pop**: Removes elements one by one using `array.pop()`. 2. **shift**: Removes elements one by one using `array.shift()`. 3. **length = 0**: Sets the length of the array to 0. 4. **reassignment**: Reassigns the array variable to an empty array `[]`. 5. **splice(0)**: Uses the `.splice(0)` method to remove the first element. **Comparison** The benchmark compares these five approaches to clear an array: * **pop** and **shift** are similar, but pop removes elements from the end of the array, while shift removes elements from the beginning. * **length = 0** is a simple and efficient way to set the length to 0, but it doesn't remove any existing elements. * **reassignment** creates a new empty array, effectively discarding the original one. This approach can be slower due to the creation of a new object. * `.splice(0)` removes the first element from the array and shifts all other elements down by one position. **Pros/Cons** Here's a brief summary: * **pop**: + Pros: Simple, efficient, and widely supported. + Cons: Can be slower than shift for large arrays. * **shift**: + Pros: Faster than pop for large arrays due to fewer DOM mutations. + Cons: Removes elements from the beginning of the array. * **length = 0**: + Pros: Fast, efficient, and widely supported. + Cons: Doesn't remove any existing elements. * **reassignment**: + Pros: Creates a new empty array, effectively discarding the original one. + Cons: Can be slower due to object creation. * `.splice(0)`: + Pros: Removes the first element efficiently. + Cons: Can cause DOM mutations and slow down other elements in the array. **Library and Special Features** There is no specific library mentioned in this benchmark. However, some libraries like Lodash or Underscore.js might provide alternative methods for clearing arrays, such as `_.isEmpty()` or `_.unset()`. If you're interested in exploring these alternatives, I can provide more information. Keep in mind that this benchmark is designed to test basic JavaScript functionality and may not cover all edge cases.
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
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?