var size = 100000;
var a = [];
for (var i = 0; i < size; i++){
a.push(Math.random());
}
var json = JSON.stringify(a);
var a1 = JSON.parse(json);
a1 = [];
var a2 = JSON.parse(json);
a2.length = 0;
var a3 = JSON.parse(json);
a3.splice(0, a3.length);
var a4 = JSON.parse(json);
while(a4.length > 0){
a4.pop();
}
var a0 = JSON.parse(json);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Instantiate new array | |
Set length to zero | |
Splice at index zero | |
Pop all values | |
JSON.parse baseline |
Test name | Executions per second |
---|---|
Instantiate new array | 105.9 Ops/sec |
Set length to zero | 107.2 Ops/sec |
Splice at index zero | 116.0 Ops/sec |
Pop all values | 110.4 Ops/sec |
JSON.parse baseline | 108.7 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmark that measures the performance of different approaches to empty an array of 100,000 random numbers.
Benchmark Definition
The benchmark definition is as follows:
for
loop.var a1 = JSON.parse(json); a1 = [];
)var a2 = JSON.parse(json); a2.length = 0;
)splice
to remove elements from the beginning of the array (var a3 = JSON.parse(json); a3.splice(0, a3.length);
)while
loop (var a4 = JSON.parse(json); while(a4.length > 0){a4.pop();}
)Options Compared
The benchmark compares the performance of four different approaches to empty an array:
var a1 = JSON.parse(json); a1 = [];
)var a2 = JSON.parse(json); a2.length = 0;
)splice
to remove elements from the beginning of the array (var a3 = JSON.parse(json); a3.splice(0, a3.length);
)while
loop (var a4 = JSON.parse(json); while(a4.length > 0){a4.pop();}
)Pros and Cons of Each Approach
splice
to remove elements from the beginning of the array. It is similar to setting the length to zero but avoids the potential issues with modifying an array in place.while
loop. It can lead to issues with modifying an array in place.Library Used
The benchmark uses JSON.parse
to create a new JavaScript object from a JSON string. This is used as a baseline comparison for the four emptying methods.
Special JS Feature/Syntax
None of the benchmark test cases use special JS features or syntax that require additional explanation.
Other Alternatives
If you need to empty an array in JavaScript, consider using one of the following alternatives:
Array.prototype.fill()
: This method fills all elements of an array with a specified value. It can be used to clear an array by setting its length to zero.Array.prototype.slice()
: This method creates a shallow copy of a portion of an array. It can be used to create a new array that contains the same elements as the original array, but it does not modify the original array.In summary, the benchmark compares four different approaches to empty an array in JavaScript: instantiating a new array, setting the length to zero, using splice
at index zero, and looping through the array and popping each element. Each approach has its pros and cons, and the choice of method depends on the specific use case and requirements.