var count = 10000;
var target = [];
for(let i = 0; i < count; i++) {
target[i] = i;
}
var sum = 0;
var i = 0;
while(sum < 70000) {
sum += target[i];
i++;
}
var sum = 0;
for(let i = 0; sum < 70000; i++) {
sum += target[i];
}
var sum = 0;
var i = 0;
var sumFunction = (num) => {
sum += target[num];
i++;
if(sum < 70000) sumFunction(i);
};
sumFunction(i);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
while roop | |
for | |
call myself |
Test name | Executions per second |
---|---|
while roop | 91320.3 Ops/sec |
for | 91455.2 Ops/sec |
call myself | 61779.1 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark defines three test cases: while roop
, for
, and call myself
. These test cases compare the performance of different JavaScript loop constructs.
What is tested?
In this benchmark, the following options are compared:
while roop
): This test case uses a traditional while loop to iterate through an array.for
): This test case uses a for loop with an iterator to iterate through the same array as the while loop test case.call myself
): This test case uses a recursive function call to achieve the same iteration as the other two test cases.Pros and Cons of Each Approach
While Loop (while roop
)
For Loop (for
)
Call Myself (call myself
)
Other Considerations
Library Usage
None of the provided benchmark test cases explicitly uses a library. However, it's worth noting that libraries like lodash
or underscore
might be used in real-world applications for array iteration and manipulation. In this case, they would likely provide an additional layer of abstraction and optimization.
Special JS Features or Syntax
The test cases do not use any special JavaScript features or syntax beyond the standard language constructs mentioned earlier.