const arr1 = new Array(100).fill().map(()=>new Array(100).fill().map(()=>100));
const arr3 = new Array(100);
for (let i=0;i<100;i++) {
arr3[i] = new Array(100);
for (let j=0;j<100;j++) {
arr3[i][j] = 100;
}
}
const arr4 = new Uint8Array(100);
for (let i=0;i<100;i++) {
arr4[i] = new Array(100);
for (let j=0;j<100;j++) {
arr4[i][j] = 100;
}
}
const arr5 = [];
for (let i=0;i<100;i++) {
const row = [];
for (let j=0;j<100;j++) {
row.push(100);
}
arr5.push([row]);
}
const arr6 = [];
for (let i=0;i<100;i++) {
const row = [];
for (let j=0;j<100;j++) {
row.push(100);
}
arr6.push(row);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Functional | |
New array | |
Uint8 array | |
Push destructured row | |
Push row (closure) |
Test name | Executions per second |
---|---|
Functional | 26676.0 Ops/sec |
New array | 38425.1 Ops/sec |
Uint8 array | 683.2 Ops/sec |
Push destructured row | 45076.6 Ops/sec |
Push row (closure) | 53618.3 Ops/sec |
Measuring the performance of JavaScript benchmarks is crucial to understand the efficiency of different approaches in various scenarios.
Benchmark Definition JSON
The provided benchmark definition json represents a 2D array initialization task, which is used as the basis for comparing different methods of creating and populating a 2D array.
Test Cases
There are four test cases that compare different approaches to create and populate a 2D array:
Array.prototype.map()
twice to create a 2D array.new Array(100)
and then populates it with inner arrays using a nested loop.Uint8Array
instead of regular arrays to create a 2D array.Other Considerations
const
, let
, and arrow functions does not appear to have a significant impact on performance in this benchmark.Latest Benchmark Results
The provided latest benchmark results show that:
new Array()
with Uint8Array can be an efficient way to create a 2D array.Overall, this benchmark highlights the importance of considering performance optimization when writing JavaScript code.