var size = 15;
const array = [];
array.length = size;
let index = array.length;
while (index-- > 0) {
const temp = [];
temp.length = size;
array[index] = temp;
}
const array = [];
array.length = size;
let index = array.length;
const temp = [];
temp.length = size;
while (index-- > 0) array[index] = Array.from(temp);
const array = [];
array.length = size;
let index = array.length;
const temp = [];
temp.length = size;
while (index-- > 0) array.push(Array.from(temp));
const array = [];
array.length = size;
let index = size;
while (index-- > 0) {
const temp = [];
temp.length = size;
array.push(temp);
}
const array = Array.from(Array(size), () => {
const temp = [];
temp.length = size;
return temp;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
While Loop with declaration | |
While Loop with Array.from | |
While Loop with Push and Array.From | |
While Loop with Push | |
Array.from with map |
Test name | Executions per second |
---|---|
While Loop with declaration | 303659.9 Ops/sec |
While Loop with Array.from | 303098.6 Ops/sec |
While Loop with Push and Array.From | 274984.6 Ops/sec |
While Loop with Push | 283912.2 Ops/sec |
Array.from with map | 218462.0 Ops/sec |
The provided JSON represents a benchmark for creating a 2D array in JavaScript using different approaches. Let's break down each test case and compare the options.
Benchmark Definition The benchmark definition describes creating a 2D "square" array filled with arrays of the same length.
Script Preparation Code
The script preparation code sets the size of the 2D array to be created: var size = 15;
.
Html Preparation Code There is no HTML preparation code provided.
Individual Test Cases
let index = array.length;
while (index-- > 0) {
const temp = [];
temp.length = size;
array[index] = temp;
}
Pros: Simple and straightforward.
Cons: May be slower due to the use of array[index]
, which can lead to a lot of lookups, and the variable declaration can lead to more memory allocation.
let index = array.length;
while (index-- > 0) {
const temp = [];
temp.length = size;
array[index] = Array.from(temp);
}
Pros: More concise and expressive than the traditional while loop.
Cons: May be slower due to the use of Array.from
, which can lead to more memory allocation, and it may not perform as well for large arrays.
let index = size;
while (index-- > 0) {
const temp = [];
temp.length = size;
array.push(Array.from(temp));
}
Pros: More concise than the traditional while loop.
Cons: May be slower due to the use of array.push
, which can lead to more memory allocation, and it may not perform as well for large arrays.
let index = size;
while (index-- > 0) {
const temp = [];
temp.length = size;
array.push(temp);
}
Cons: May be slower due to the use of array.push
, which can lead to more memory allocation, and it may not perform as well for large arrays.
const array = Array.from(Array(size), () => {
const temp = [];
temp.length = size;
return temp;
});
Pros: Most concise and expressive, as it uses a single line of code.
Cons: May be slower due to the use of Array.from
with map, which can lead to more memory allocation, and it may not perform as well for large arrays.
Library
In this benchmark, the Array.from()
method is used in three test cases. The purpose of Array.from()
is to create a new array by mapping an existing iterable (such as an array or string) to a new array with the specified length.
Special JS Features There are no special JavaScript features mentioned in this benchmark.
Other Alternatives
In addition to the five test cases provided, other alternatives for creating a 2D array could include:
Array.fill()
and map()
: array.map((_, i) => Array(size).fill().map(() => []))
Array.prototype.reduce()
: array.reduce((acc, _, i) => ({ ...acc, [i]: Array(size).fill().map(() => []) }), {})
These alternatives may have different performance characteristics compared to the test cases provided.