let arr = Array(1000).fill(undefined).map(e => Math.floor(Math.random()*1000000+1));
let arr = [];
for (i=0; i<1000001; i++){arr.push(Math.floor(Math.random()*1000000+1))}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Set-length array | |
Array.push |
Test name | Executions per second |
---|---|
Set-length array | 836.4 Ops/sec |
Array.push | 1.1 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
Benchmark Definition
The benchmark definition is a JSON object that provides metadata about the test case. In this case, there are two benchmark definitions:
Fill array with random integers
: This benchmark definition describes the test scenario, where an array of 1000 elements is filled with random integers between 1 and 999,999 (inclusive).null
), which means that no specific code needs to be executed before running the test.null
), indicating that no special HTML setup is required.Individual Test Cases
There are two individual test cases:
Set-length array
: This test case uses a concise syntax to create an array of 1000 elements using Array(1000).fill(undefined).map(e => Math.floor(Math.random()*1000000+1))
.Array.push
: This test case uses a traditional for
loop to push 1000 random integers onto an empty array (let arr = [];\r\nfor (i=0; i<1000001; i++){arr.push(Math.floor(Math.random()*1000000+1))}
).Library and Purpose
Neither of the test cases uses any external libraries. The Array
constructor is a built-in JavaScript function, and the Math.random()
function is also a built-in function.
Special JS Features or Syntax
There are no special JavaScript features or syntax being used in either test case.
Pros and Cons of Different Approaches
fill()
method.Other Alternatives
If you were to create a similar benchmark, other alternatives for the set-length array
approach could include:
Array.from()
: Array.from(new Array(1000), () => Math.floor(Math.random()*1000000+1))
repeat()
function: _._.repeat(1000)(Math.floor(Math.random()*1000000+1))
For the Array.push
approach, alternative methods could include:
splice()
: let arr = []; for (i=0; i<1000001; i++){arr.splice(i, 0, Math.floor(Math.random()*1000000+1))}
repeat()
function with the push()
method: _._.forEach(1000)(arr.push, Math.floor(Math.random()*1000000+1))
Keep in mind that these alternatives may not necessarily change the outcome of the benchmark, but they could provide additional insights into different aspects of JavaScript performance.