Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
clearing array via pop() vs shift() vs .length = 0 vs = [] 1
(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 < 10000; i++){array.push(Math.random());}
Tests:
pop
let a = [...array]; while (a.length > 0) { a.pop(); }
shift
let a = [...array]; while (a.length > 0) { a.shift(); }
length
let a = [...array]; a.length = 0
reassignment
let a = [...array]; a = []
.splice(0)
let a = [...array]; a.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:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
pop
26383.7 Ops/sec
shift
10348.7 Ops/sec
length
35743.2 Ops/sec
reassignment
41087.9 Ops/sec
.splice(0)
37111.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark definition, which outlines the test scenario. In this case, we're comparing four different ways to clear an array: 1. Using `pop()` method 2. Using `shift()` method 3. Setting `length` property to 0 4. Reassigning the array to an empty array (`[]`) The benchmark prepares an array of length 10000 and pushes random numbers onto it for each iteration. The test then measures the execution time for each approach to clear the array. **Options Compared** We have four options being compared: 1. `pop()`: Removes and returns the last element from the array. 2. `shift()`: Removes and returns the first element from the array. 3. Setting `length` property to 0: Sets the length of the array to 0, effectively clearing it. 4. Reassigning the array to an empty array (`[]`): Creates a new array and assigns it to the original variable. **Pros and Cons** Here's a brief summary of each approach: 1. `pop()`: * Pros: Efficient in terms of memory usage (only removes one element at a time). * Cons: Slower than other methods, as it involves multiple iterations. 2. `shift()`: * Pros: Similar to `pop()`, efficient in terms of memory usage. * Cons: Also slower due to the need for multiple iterations. 3. Setting `length` property to 0: * Pros: Fast and efficient, as it directly modifies the array's length. * Cons: Does not modify the array's content; only its size is affected. 4. Reassigning the array to an empty array (`[]`): * Pros: Creates a new array with no elements, freeing up memory. * Cons: Slow due to the overhead of creating a new array and assigning it to the original variable. **Library Usage** The benchmark uses the `Math.random()` function, which is a built-in JavaScript function for generating random numbers. No external libraries are required. **Special JS Features/Syntax** There's no special JavaScript feature or syntax being tested in this benchmark. The focus is on comparing different array manipulation approaches. **Other Alternatives** If you're interested in exploring alternative ways to clear an array, here are a few more: 1. Using `splice()` method with arguments (e.g., `array.splice(0)`): Removes elements from the beginning of the array. 2. Using `slice()` method with negative start index (e.g., `array.slice(-10000)`): Creates a new array containing the last 10,000 elements. 3. Using `fill()` method to create a new array filled with a specific value. Keep in mind that these alternatives may have different performance characteristics or memory usage compared to the original four methods being tested.
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)
Comments
Confirm delete:
Do you really want to delete benchmark?