var size = 1000000;
var arr1 = [];
for (var i = 0; i < size; i++){
arr1.push(i);
}
arr1 = [];
var size = 1000000;
var arr2 = [];
for (var i = 0; i < size; i++){
arr2.push(i);
}
arr2.length = 0;
var size = 1000000;
var arr3 = [];
for (var i = 0; i < size; i++){
arr3.push(i);
}
arr3.splice(0, arr3.length);
var size = 1000000;
var arr4 = [];
for (var i = 0; i < size; i++){
arr4.push(i);
}
while(arr4.length > 0){
arr4.pop();
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Just instantiate new array | |
Set length to zero | |
Splice | |
Pop all values |
Test name | Executions per second |
---|---|
Just instantiate new array | 26.5 Ops/sec |
Set length to zero | 36.1 Ops/sec |
Splice | 25.3 Ops/sec |
Pop all values | 18.3 Ops/sec |
What is tested on the provided JSON?
The provided JSON represents four benchmark test cases that compare different approaches to empty an array in JavaScript. The test cases are:
arr1
, then delete all elements from arr1
.arr2
to 0, which also deletes all elements from arr2
.splice()
method to remove all elements from arr3
.pop()
to delete all elements from arr4
.Options compared
Each test case compares two different approaches:
Pros and Cons of each approach
arr1
.pop()
.Library
None of the provided benchmark test cases use any external libraries. However, JavaScript's built-in array methods (e.g., splice()
, length
) are used in each test case.
Special JS feature or syntax
No special features or syntax are mentioned in the provided JSON. The tests only use standard JavaScript syntax and built-in array methods.
Other alternatives
Some alternative approaches to empty an array in JavaScript include:
fill()
method with a length of 0: arr.fill(0, 0, arr.length);
Array.prototype.fill()
with a length of 0: new Array(arr.length).fill(0);
These alternatives may offer performance benefits or more intuitive approaches than the ones tested in this benchmark.
Keep in mind that benchmarking JavaScript performance can be complex and influenced by various factors, such as browser-specific optimizations and hardware.