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] = temp.slice(0);
const array = [];
array.length = size;
let index = array.length;
const temp = [];
temp.length = size;
while (index-- > 0) array.push(temp.slice(0));
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;
});
const temp = [];
temp.length = size;
const array = Array.from(temp, () => temp.slice());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
While Loop with declaration | |
While Loop with Slice | |
While Loop with Push and Slice | |
While Loop with Push | |
Array.from with map | |
Array.from with map and slice |
Test name | Executions per second |
---|---|
While Loop with declaration | 285314.0 Ops/sec |
While Loop with Slice | 1484854.5 Ops/sec |
While Loop with Push and Slice | 1377482.8 Ops/sec |
While Loop with Push | 282811.1 Ops/sec |
Array.from with map | 225431.3 Ops/sec |
Array.from with map and slice | 667623.4 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark for creating a 2D square array filled with arrays of the same length.
Benchmark Definition
The benchmark definition consists of two parts:
size
and assigns it a value of 15.Individual Test Cases
There are six individual test cases that compare different approaches to creating a 2D square array:
let index = array.length;
while (index-- > 0) {
const temp = [];
temp.length = size;
array[index] = temp;
}
This approach uses a while loop to create each inner array and assigns it to the corresponding index in the outer array.
let index = array.length;
while (index-- > 0) {
const temp = [];
temp.length = size;
array[index] = temp.slice(0);
}
This approach uses a while loop to create each inner array, and then slices the newly created array to assign it to the corresponding index in the outer array.
let index = size;
while (index-- > 0) {
const temp = [];
temp.length = size;
array.push(temp.slice(0));
}
This approach uses a while loop to create each inner array, slices the newly created array, and then pushes it onto the outer array using push
.
let index = size;
while (index-- > 0) {
const temp = [];
temp.length = size;
array.push(temp);
}
This approach uses a while loop to create each inner array and pushes it onto the outer array using push
.
const array = Array.from(Array(size), () => {
const temp = [];
temp.length = size;
return temp;
});
This approach uses Array.from()
to create an array of inner arrays, where each inner array is created using the provided callback function.
const array = Array.from(temp, () => temp.slice());
This approach uses Array.from()
to create an array of inner arrays, but instead of creating a new array for each iteration, it reuses the existing temp
array by slicing it.
Options Compared
The benchmark compares six different approaches to creating a 2D square array:
Each approach has its own pros and cons:
Pros and Cons
slice()
).Array.from()
and a callback function.Conclusion
The benchmark provides insight into the performance characteristics of different approaches to creating a 2D square array in JavaScript. While Loop with Push appears to be the fastest approach, it also has the most complex logic. The Array.from()
methods provide a good balance between performance and simplicity. Ultimately, the choice of approach depends on personal preference, coding style, and specific use case requirements.