let param = [];
for (let a = 0; a < 50; a++) {
param[a] = new Array(50);
for (let b = 0; b < 50; b++) {
param[a][b] = Math.random();
}
}
let ans = [];
for (let i = 0; i < param.length; i++) {
ans.push(param[i].slice());
}
let param = [];
for (let a = 0; a < 50; a++) {
param[a] = new Array(50);
for (let b = 0; b < 50; b++) {
param[a][b] = Math.random();
}
}
let ans = [];
for (let i = 0; i < 50; i++) {
ans.push([param[i]]);
}
let param = [];
for (let a = 0; a < 50; a++) {
param[a] = new Array(50);
for (let b = 0; b < 50; b++) {
param[a][b] = Math.random();
}
}
let ans = [];
for (let i = 0; i < 50; i++) {
ans.push([]);
for(let j = 0; j < 50; j++) {
ans[i].push(param[i][j]);
}
}
let param = [];
for (let a = 0; a < 50; a++) {
param[a] = new Array(50);
for (let b = 0; b < 50; b++) {
param[a][b] = Math.random();
}
}
let ans = [];
for (let i = 0; i < 50; i++) {
ans.push([]);
for(let j = 0; j < 50; j++) {
ans[i].push(param[i][j]);
}
}
let param = [];
for (let a = 0; a < 50; a++) {
param[a] = new Array(50);
for (let b = 0; b < 50; b++) {
param[a][b] = Math.random();
}
}
let ans = [];
for (let i = 0; i < 50; i++) {
ans.push([]);
for(let j = 0; j < 50; j++) {
ans[i][j] = param[i][j];
}
}
let param = [];
for (let a = 0; a < 50; a++) {
param[a] = new Array(50);
for (let b = 0; b < 50; b++) {
param[a][b] = Math.random();
}
}
let ans = new Array(50);
for (let i = 0; i < 50; i++) {
ans[i] = new Array(50);
for(let j = 0; j < 50; j++) {
ans[i][j] = param[i][j];
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.slice | |
spread operator | |
simple loop | |
constants loop | |
placement loop | |
pre-built array loop |
Test name | Executions per second |
---|---|
Array.prototype.slice | 3662.0 Ops/sec |
spread operator | 3858.1 Ops/sec |
simple loop | 3821.4 Ops/sec |
constants loop | 3761.7 Ops/sec |
placement loop | 3803.8 Ops/sec |
pre-built array loop | 3860.3 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and analyze the provided benchmark.
Benchmark Definition
The benchmark compares three approaches to creating a 2D array: Array.prototype.slice
, spread operator (...
), simple loop, constants loop, placement loop, and pre-built array loop. The goal is to determine which approach is the fastest.
Options Compared
slice()
method to create a shallow copy of the 2D array....
): Uses the spread operator to create a new array with the elements of the original 2D array.Key Insights
Array.prototype.slice
because it creates a new array by simply copying the elements, whereas slice()
creates a shallow copy of the original array, which can be slower.Latest Benchmark Results
The latest results show that:
...
): 3858.05859375 executions per secondThese results indicate that the pre-built array loop is still the slowest, while the Array.prototype.slice
method is slightly faster than expected.
Conclusion
In summary, the benchmark highlights the differences in performance between various approaches to creating a 2D array in JavaScript. The spread operator and Array.prototype.slice
methods are generally faster than simple loops or placement loops, which avoid creating intermediate arrays. However, the pre-built array loop is still the slowest due to its overhead.