function generateLargeTestRef() {
const testRef = {};
for (let i = 0; i < 1; i++) {
testRef[`prop${i}`] = "x".repeat(2); // 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 | 359046.1 Ops/sec |
forEach 2 | 361259.8 Ops/sec |
for loop 1 | 63181.8 Ops/sec |
for loop 2 | 63584.4 Ops/sec |
for loop 1-2 | 88896.0 Ops/sec |
for loop 2-2 | 87808.6 Ops/sec |
for-in | 7859.4 Ops/sec |
for-in 2 | 7932.0 Ops/sec |
for-of | 394819.3 Ops/sec |
for-of 2 | 395288.9 Ops/sec |
This benchmark evaluates different methods of iterating through arrays of objects in JavaScript, specifically focusing on two array setups (testObject and testObject2), each consisting of 10,000 elements. Each element is an object with references to a large object testRef_large
, which is created by the generateLargeTestRef
function. The benchmark tests the following iteration techniques:
forEach 1
: Iterating through testObject
forEach 2
: Iterating through testObject2
for loop 1
: Basic for loop through testObject
for loop 2
: Basic for loop through testObject2
for loop 1-2
: For loop using a length variable with testObject
for loop 2-2
: For loop using a length variable with testObject2
break
.for-in
: Iterating through testObject
for-in 2
: Iterating through testObject2
for-of
: Iterating through testObject
for-of 2
: Iterating through testObject2
for
loops.Based on the benchmark results in the provided JSON, for-of
performed the best followed closely by forEach
methods. The traditional for
loops showed significantly lesser performance, indicating that their more verbose setup may involve additional overhead during execution. The for-in
loop showed the poorest results as it is not intended for iterating through arrays, reinforcing the recommendation against its use for such cases.
This benchmark provides a useful overview for software engineers on how different iteration methods can impact performance, especially in JavaScript. The findings suggest using for-of
or forEach
for readability in most cases, while traditional for
may be used when maximum performance is critical.
Other iteration constructs or methods not tested here include:
Array methods like .map()
, .filter()
, and .reduce()
: These methods provide a functional programming style and can be insightful for creating new arrays or reducing values, but typically involve more overhead than pure iteration.
While and do-while loops: Additional looping mechanisms that can be used similarly to the for
loop.
Using external libraries (like Lodash): Libraries often provide methods that may optimize iteration tasks, especially with very complex data manipulation scenarios.
Overall, the choice of iteration method should be guided by a balance of performance, readability, and the specific requirements of the task at hand.