const i = 25;
const j = 25;
const a = Array.from(new Array(i), () => new Array(j))
const i = 25;
const j = 25;
const a = Array.from({length:i}, () => new Array(j))
const i = 25;
const j = 25;
const a = []
for (let k = 0; k < i; k++) {
a.push(new Array(j));
}
const i = 25;
const j = 25;
const a = []
for (let k = 0; k < i; k++) {
a[k] = new Array(j);
}
const i = 25;
const j = 25;
const a = Array.from(Array(i), () => new Array(j))
const i = 25;
const j = 25;
const a = []
a.length = i;
for (let k = 0; k < i; k++) {
a.push(new Array(j));
}
const i = 25;
const j = 25;
const a = []
a.length = i;
for (let k = 0; k < i; k++) {
a[k] = new Array(j);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.from(new Array(length), () => new Array(length)) | |
Array.from({length}, () => new Array(length)) | |
for push | |
for index | |
Array.from(Array(length), () => new Array(length)) | |
for push set size | |
for index set size |
Test name | Executions per second |
---|---|
Array.from(new Array(length), () => new Array(length)) | 160453.8 Ops/sec |
Array.from({length}, () => new Array(length)) | 150472.2 Ops/sec |
for push | 227218.6 Ops/sec |
for index | 239894.0 Ops/sec |
Array.from(Array(length), () => new Array(length)) | 161594.3 Ops/sec |
for push set size | 221498.6 Ops/sec |
for index set size | 233617.0 Ops/sec |
Benchmark Overview
The provided benchmark is designed to compare the performance of different approaches for creating nested arrays in JavaScript. The test cases cover various scenarios, including using Array.from()
, loops (for push and for index), and an object literal.
Tested Options
Array.from(new Array(length), () => new Array(length))
: Creates a 2D array by mapping over a one-dimensional array of the specified length.Array.from({length:i}, () => new Array(j))
: Creates a 2D array by mapping over an object literal with a specified length and mapping each element to a 1D array of the same length as j
.for push set size
: Initializes an array with a specified size and then pushes arrays onto it using a for loop.for index set size
: Initializes an array with a specified size and then initializes each element using a for loop.Comparison of Approaches
Array.from()
approach is generally faster than the loop-based approaches, especially for larger arrays. This is because Array.from()
uses optimized internal logic to create arrays efficiently.for push
and for index
approaches allocate memory for each array individually, which can lead to slower performance due to the overhead of repeated memory allocations. In contrast, the Array.from()
approach allocates memory all at once.Library
None of the tested options rely on a specific library or framework.
Special JavaScript Features/Syntax
None of the tested options use special JavaScript features or syntax, such as async/await, generators, or classes.
Alternatives
Some alternatives to the Array.from()
approach include:
Array.prototype.map()
: Similar to Array.from()
, but returns a new array with the mapped values, rather than an array of arrays.Array.from()
in some cases.Overall, the choice of approach depends on the specific requirements and constraints of your project. If performance is critical, Array.from()
may be the best option. However, if readability and maintainability are more important, a loop-based approach might be a better choice.