Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
clearing array via pop() vs shift() vs .length = 0 vs = [] vs splice()
(version: 0)
Comparing performance of:
pop vs shift vs length vs reassignment vs splice
Created:
4 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
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
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.2:3b
, generated one year ago):
The provided JSON represents a JavaScript benchmark test case that compares the performance of different methods to clear an array in JavaScript. **What is being tested?** The test cases compare the following methods: 1. `pop()`: Removing elements from the end of the array using `array.pop()`. 2. `shift()`: Removing elements from the beginning of the array using `array.shift()`. 3. `length = 0` (or `array.length = 0`): Setting the length of the array to zero, effectively clearing it. 4. `array = []` (or `let array = []`, not shown in this example): Reassigning a new empty array to the variable. 5. `splice(0)` (or `array.splice(0)`: Removing elements from the beginning of the array using `array.splice(0)`. **Options comparison** Here's a brief summary of each method: 1. **`pop()`**: This method removes elements from the end of the array and returns them as an array. It can be slower than other methods because it creates a new array. 2. **`shift()`**: Similar to `pop()`, but removes elements from the beginning of the array. Again, this may create a new array internally. 3. **`length = 0`**: This method sets the length of the array to zero without creating a new array or modifying its content in place. 4. **`array = []`**: Reassigning a new empty array to the variable effectively clears the original array, as arrays are reference types and simply reassigning the variable doesn't change the underlying storage. **Pros and cons** 1. `pop()` and `shift()`: These methods can be slower because they create new arrays internally. 2. `length = 0` (or `array.length = 0`): This method is generally faster since it doesn't create a new array, but it may cause issues if you're using a prototype chain or other advanced JavaScript features that rely on the original length being preserved. 3. `array = []`: Reassigning an empty array can be faster than `length = 0`, but it only works for reassigning the variable to a new location. **Special JS feature** In this benchmark, none of the methods require special JavaScript features like async/await or ES6+ classes. However, the use of arrays with length attributes and prototype chains might be specific to certain browsers or environments that support older JavaScript versions. **Other alternatives** If you need even more efficient ways to clear an array, consider using: 1. `Array.prototype.fill()` (for clearing a subset of elements) 2. `Array.from()` (for creating a new array from scratch) These methods can be faster than the ones tested in this benchmark, especially for large arrays or performance-critical code. Keep in mind that these alternatives might not work with older browsers or environments that support older JavaScript versions. If you need to ensure compatibility across various browsers and environments, sticking with `length = 0` (or `array.length = 0`) is a reliable choice.
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 .length = 0 vs. = [] vs. .splice(0)
Empty array: Splice vs Shift vs Pop
Comments
Confirm delete:
Do you really want to delete benchmark?