var array = new Array(100);
for (var i = 0; i < array.length; i++) {
array[i];
}
array.forEach(function(i) {
array[i];
});
array.some(function(i) {
array[i];
});
for (var i of array) {
array[i];
}
for (var i in array) {
array[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for | |
foreach | |
some | |
for..of | |
for..in |
Test name | Executions per second |
---|---|
for | 128341.1 Ops/sec |
foreach | 3683501.5 Ops/sec |
some | 4085749.5 Ops/sec |
for..of | 221696.9 Ops/sec |
for..in | 5266972.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
MeasureThat.net is testing the performance of different loop constructs in JavaScript:
for
loopsforEach
loops (an array method)some
loops (an array method)for..of
loops (a newer, more concise syntax for iterating over arrays)These loops are used to iterate over an array of 100 elements. The performance test measures how many executions per second each loop can handle.
Options being compared:
The test compares the performance of four different loop constructs:
for
loopsforEach
loops (an array method)some
loops (an array method)for..of
loopsEach option has its pros and cons:
for
loops.forEach
, but returns a boolean result indicating whether at least one element matches the condition.Library usage:
None of the provided test cases use any external libraries. They rely solely on built-in JavaScript functionality.
Special JS features or syntax:
There is no mention of special JavaScript features or syntax in this benchmark definition. The tests focus on comparing different loop constructs without introducing any additional complexities.
Alternative approaches:
If you were to design an alternative test, you might consider including other loop constructs, such as:
while
loopsAdditionally, you could explore testing different array sizes or edge cases (e.g., empty arrays, null or undefined elements) to further validate the performance of each loop construct.