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: 10 }, () => ({
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: 10 }, () => ({
a: testRef_large,
b: testRef_large,
c: testRef_large,
e: testRef_large,
f: testRef_large,
}));
testObject.forEach(item => {
console.log(item)
});
testObject2.forEach(item => {
console.log(item)
});
for (let i = 0; i < testObject.length; i++) {
console.log(testObject[i])
}
for (let i = 0; i < testObject2.length; i++) {
console.log(testObject2[i])
}
for (let i = 0, l = testObject.length; i < l; i++) {
console.log(testObject[i])
}
for (let i = 0, l = testObject2.length; i < l; i++) {
console.log(testObject2[i])
}
for (let key in testObject) {
console.log(testObject[key])
}
for (let key in testObject2) {
console.log(testObject2[key])
}
for (let item of testObject) {
console.log(item) }
for (let item of testObject2) {
console.log(item) }
--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 | 18697.6 Ops/sec |
forEach 2 | 18258.6 Ops/sec |
for loop 1 | 21148.3 Ops/sec |
for loop 2 | 16394.5 Ops/sec |
for loop 1-2 | 18426.3 Ops/sec |
for loop 2-2 | 17763.3 Ops/sec |
for-in | 20941.2 Ops/sec |
for-in 2 | 18053.4 Ops/sec |
for-of | 18920.0 Ops/sec |
for-of 2 | 19887.9 Ops/sec |
The provided benchmark JSON describes a series of performance tests for different JavaScript iteration methods applied to collections of objects. This setup can help developers analyze the efficiency of various approaches to iterating through arrays and objects in JavaScript. Here’s a breakdown of what is being tested, the options compared, and their pros and cons.
testRef_large
) is generated, containing a single property that holds a string of 1 million characters.testObject
: 10 objects, each referencing testRef_large
three times (properties a
, b
, and c
).testObject2
: 10 objects, referencing testRef_large
five times (properties a
, b
, c
, e
, and f
).The benchmark consists of multiple methods of iterating over these arrays:
forEach:
forEach 1
: Iterates over testObject
.forEach 2
: Iterates over testObject2
.for loop:
for loop 1
: Standard for loop iterating testObject
.for loop 2
: Standard for loop iterating testObject2
.for loop 1-2
and for loop 2-2
: Using a cached length to avoid recalculating on every iteration.for-in loop:
for-in
: Iterates over keys of testObject
.for-in 2
: Iterates over keys of testObject2
.for
loops for arrays.for-of loop:
for-of
: Iterates over values of testObject
.for-of 2
: Iterates over values of testObject2
.for
loop and can be more verbose if you're just iterating over indices.ExecutionsPerSecond
for each method, which helps determine the most efficient iteration technique in this context. The benchmarks reveal the relative speeds of each approach, where traditional for
loops generally outperform array methods (forEach
).When choosing an iteration method:
forEach
or for-of
for cleaner syntax and ease of maintenance, especially if performance is not critical.for
loop is a common choice due to its universal support and optimization across all JavaScript engines. for
loops might be the best option.In addition to the methods tested, other alternatives include:
forEach
.Overall, this benchmark suite effectively emphasizes the differences in execution time among various JavaScript iteration techniques, aiding developers in making informed optimization choices based on their specific use case.