var array = new Array(1000000).fill(0);
function getObj() {
return {x: 0, Y: 0};
}
for (let i = 0; i < array.length; i++) array[i] = getObj();
for (let i = 0; i < array.length; ++i) array[i] = getObj();
const l = array.length;
for (let i = 0; i < l; ++i) array[i] = getObj();
for (const i in array) array[i] = getObj();
let i;
for (i in array) array[i] = getObj();
const arr2 = array.map(_ => getObj());
const arr3 = [];
for (let i = 0; i < 1000000; i++) arr3.push(getObj());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 |
Test name | Executions per second |
---|---|
1 | 6.6 Ops/sec |
2 | 6.6 Ops/sec |
3 | 9.8 Ops/sec |
4 | 5.1 Ops/sec |
5 | 5.2 Ops/sec |
6 | 16.5 Ops/sec |
7 | 14.4 Ops/sec |
Let's break down the provided JSON benchmark and explain what's being tested.
Benchmark Definition
The benchmark is testing how quickly JavaScript arrays can be populated with objects using different methods.
Options Compared
There are six options compared:
for (let i = 0; i < array.length; i++) array[i] = getObj();
for (let i = 0; i < array.length; ++i) array[i] = getObj();
const l = array.length; for (let i = 0; i < l; ++i) array[i] = getObj();
for (const i in array) array[i] = getObj();
let i; for (i in array) array[i] = getObj();
const arr2 = array.map(_ => getObj());
Pros and Cons of Each Approach
i
variable.i
.in
operator to loop through the array's own enumerable properties (not indices).map()
applies a function to each element in an array and returns a new array with the results.Other Considerations
getObj()
function creates objects on each iteration, which may lead to additional memory allocations and garbage collection.Library Usage
There is no explicit library mentioned in the provided benchmark definition or test cases. However, some libraries might be implicitly used, such as:
console
for logging outputArray.prototype.map()
, which is a part of the JavaScript standard librarySpecial JS Features/Syntax
None are explicitly mentioned in the provided information.
I hope this explanation helps!