var size = 100000;
var a = [];
for (var i = 0; i < size; i++){
a.push(Math.random());
}
var a1 = JSON.parse(JSON.stringify(a));
a1 = [];
var a2 = JSON.parse(JSON.stringify(a));
a2.length = 0;
var a3 = JSON.parse(JSON.stringify(a));
a3.splice(0, a3.length);
var a4 = JSON.parse(JSON.stringify(a));
while(a4.length > 0){
a4.pop();
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Instantiate new array | |
Set length to zero | |
Splice at index zero | |
Pop all values |
Test name | Executions per second |
---|---|
Instantiate new array | 101.0 Ops/sec |
Set length to zero | 101.9 Ops/sec |
Splice at index zero | 101.4 Ops/sec |
Pop all values | 99.9 Ops/sec |
Let's break down the provided JSON and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The benchmark is designed to measure how fast JavaScript can empty an array of 100,000 random numbers. There are four different approaches:
a1
.a
to 0.splice()
method with an empty array and the first element as arguments (0 and a.length
).Approach Comparison
Each approach has its pros and cons:
Library and Special JS Features
In this benchmark, the library used is JSON.parse()
, which is a built-in JavaScript function. Its purpose is to parse JSON strings into JavaScript objects. However, in this case, it's being used to create a deep copy of the array using the JSON.parse(JSON.stringify())
syntax.
There are no special JS features used in this benchmark.
Test Results
The latest test results show that:
Alternatives
If you wanted to modify this benchmark, here are some alternatives:
Keep in mind that the results may vary depending on the specific use case and system configuration.