function generateLargeTestRef() {
const testRef = {};
for (let i = 0; i < 50; i++) {
testRef[`prop${i}`] = "x".repeat(10**6); // 1 million de caractères
}
return testRef;
}
// Créer le testRef_large avec 50 propriétés
const testRef_large = generateLargeTestRef();
// Créer 10 000 testObject avec 3 références à testRef_large
const testObject = Array.from({ length: 10000 }, () => ({
a: testRef_large,
b: testRef_large,
c: testRef_large,
}));
// Créer 10 000 testObject2 avec 5 références à testRef_large
const testObject2 = Array.from({ length: 10000 }, () => ({
a: testRef_large,
b: testRef_large,
c: testRef_large,
e: testRef_large,
f: testRef_large,
}));
testObject.forEach(item => {
});
testObject2.forEach(item => {
});
for (let i = 0; i < testObject.length; i++) {
const item = testObject[i];
}
for (let i = 0; i < testObject2.length; i++) {
const item = testObject2[i];
}
for (let i = 0, l = testObject.length; i < l; i++) {
const item = testObject[i];
}
for (let i = 0, l = testObject2.length; i < l; i++) {
const item = testObject2[i];
}
for (let key in testObject) { }
for (let key in testObject2) { }
for (let item of testObject) { }
for (let item of testObject2) { }
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
forEach 1 | |
forEach 2 | |
for loop 1 | |
for loop 2 | |
for loop 1-2 | |
for loop 2-2 | |
for-in | |
for-in 2 | |
for-of | |
for-of 2 |
Test name | Executions per second |
---|---|
forEach 1 | 359399.3 Ops/sec |
forEach 2 | 354638.5 Ops/sec |
for loop 1 | 63179.2 Ops/sec |
for loop 2 | 62977.0 Ops/sec |
for loop 1-2 | 87915.4 Ops/sec |
for loop 2-2 | 88056.8 Ops/sec |
for-in | 7650.0 Ops/sec |
for-in 2 | 7875.3 Ops/sec |
for-of | 394294.8 Ops/sec |
for-of 2 | 396475.8 Ops/sec |
The provided benchmark assesses various looping mechanisms in JavaScript by measuring their performance when iterating over two large data structures: testObject
and testObject2
. These objects have multiple references to a single large reference object testRef_large
containing numerous properties with substantial values.
testRef_large
): This object has 50 properties, each containing a string of 1 million characters.testObject
and testObject2
): testObject
: 10,000 instances each referencing testRef_large
three times.testObject2
: 10,000 instances each referencing testRef_large
five times.forEach:
forEach 1
: Iterates over testObject
.forEach 2
: Iterates over testObject2
.for loop:
for loop 1
: Standard for loop over testObject
.for loop 2
: Standard for loop over testObject2
.for loop 1-2
: Uses a loop variable l
to store the length of testObject
for efficiency.for loop 2-2
: Similar optimization as above for testObject2
.forEach
.for-in:
for-in
: Iterates over testObject
.for-in 2
: Iterates over testObject2
.for-of:
for-of
: Looks through testObject
.for-of 2
: Looks through testObject2
.The results indicate that the for-of
syntax performs the best in terms of executions per second across both test cases:
In contrast, the traditional for loop
methods had significantly lower performance metrics:
testRef_large
) may impact how long iterations take, especially with limited memory resources.forEach
, allows for creating new arrays from existing ones for mapping but may not impact performance significantly.Float32Array
) can significantly improve performance.This benchmark provides insights into the performance of different looping methods within JavaScript, considering readabilty, efficiency, and potential pitfalls. Software engineers can utilize these results to optimize operations that involve large data structures effectively.