const array = Array(10000);
for (let i = 0; i < 10000; i++) {
array[i] = Math.random();
}
const array = [];
for (let i = 0; i < 10000; i++) {
array.push(Math.random())
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Filling Array | |
Adding to Array |
Test name | Executions per second |
---|---|
Filling Array | 20833.9 Ops/sec |
Adding to Array | 10590.8 Ops/sec |
I'd be happy to explain the benchmark and its test cases.
What is being tested?
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON data represents two individual test cases, which compare the performance of filling an array versus adding elements to an empty array using the Math.random()
function.
Options compared:
The two test cases compare the following options:
Array(10000)
): This approach uses a pre-existing array with 10,000 elements and fills them with random values.[]
): This approach creates an empty array and adds 10,000 random elements using the push()
method.Pros and cons of each approach:
Library usage:
There is no library used in these test cases. The Array(10000)
syntax creates an array with a specified length, and the Math.random()
function generates random numbers.
Special JavaScript features or syntax:
No special JavaScript features or syntax are used in these test cases.
Other considerations:
When running microbenchmarks like this, it's essential to consider factors such as:
Alternatives:
Other alternatives for creating microbenchmarks include:
Date.now()
function to measure execution time.These alternatives can provide more detailed insights into the performance characteristics of specific browser versions and platforms.