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 => {
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 | 23.9 Ops/sec |
forEach 2 | 23.3 Ops/sec |
for loop 1 | 21.9 Ops/sec |
for loop 2 | 22.1 Ops/sec |
for loop 1-2 | 22.8 Ops/sec |
for loop 2-2 | 21.7 Ops/sec |
for-in | 21.8 Ops/sec |
for-in 2 | 22.2 Ops/sec |
for-of | 22.5 Ops/sec |
for-of 2 | 20.3 Ops/sec |
The benchmark defined in the JSON you provided tests different approaches for iterating over JavaScript objects within a specific context: it compares performance across various looping constructs using two arrays of objects (testObject
and testObject2
), each containing references to a large object (testRef_large
).
generateLargeTestRef
function creates an object (testRef_large
) with 50 properties, each containing a string of 1 million characters. This simulates a large dataset to stress test the performance of different iteration methods.testObject
and testObject2
) are constructed, each containing 10,000 objects that reference testRef_large
multiple times (3 and 5 references, respectively).The benchmark consists of various looping constructs:
forEach:
.forEach()
method to iterate over testObject
and testObject2
.for
loops because it involves a function call for each iteration.for loop:
for
loops to iterate over testObject
and testObject2
.for
loop (without caching length) and one that caches the length:for (let i = 0; i < testObject.length; i++)
for (let i = 0, l = testObject.length; i < l; i++)
for-in loop:
for (let key in testObject)
and for (let key in testObject2)
.for
loop when dealing with arrays.for-of loop:
for (let item of testObject)
.for
loop; only works with iterable objects.for
loop, and it's not suitable for iterating over non-iterable objects.Based on the benchmark results, we can observe how many iterations can be executed per second for each test:
for
variants.Other alternatives not directly tested could include:
.map
, .filter
, and .reduce
, which can provide functional programming constructs.async/await
with iteration for asynchronous operations, though that wasn't tested here.Choosing the right iteration method will depend on the context of use, data size, performance requirements, and team coding standards.