var array = [];
for(var i = 0; i < 100000; i++){array.push(Math.random());}
while (array.length > 0)
{
array.pop();
}
while (array.length > 0)
{
array.shift();
}
array.length = 0
array = []
array.splice(0);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
pop | |
shift | |
length | |
reassignment | |
.splice(0) |
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 |
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:
array.pop()
.array.shift()
.[]
..splice(0)
method to remove the first element.Comparison
The benchmark compares these five approaches to clear an array:
.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:
.splice(0)
: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.