var arrRandom = [];
for(var intCtr=0; intCtr<1000; intCtr++) {
arrRandom.push(Math.floor(Math.random() * Math.floor(10000)));
}
function doRedeuce(pArray) {
return pArray.reduce(function(accum, curr) {return accum+curr});
}
function doLoop(pArray) {
var accum = 0;
for(var intCtr=0; intCtr<pArray.length; intCtr++) {
accum += pArray[intCtr];
}
return accum;
}
function doForEach(pArray) {
var accum = 0;
pArray.forEach(function(item) {
accum += item;
});
}
var redeuceResult=0;
redeuceResult = doRedeuce(arrRandom);
var loopResult=0;
loopResult = doLoop(arrRandom);
var forEachResult=0
forEachResult = doForEach(arrRandom)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
reduce | |
for loop | |
forEach |
Test name | Executions per second |
---|---|
reduce | 1296208.2 Ops/sec |
for loop | 1371993.4 Ops/sec |
forEach | 847325.9 Ops/sec |
I'd be happy to explain the provided JSON benchmark definition and its various components.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmarking test case, specifically designed to compare the performance of three different approaches for summing an array of numbers:
reduce()
forEach()
methodThese approaches are compared to determine which one is most efficient in terms of execution time.
Benchmark Preparation Code
The benchmark preparation code is a JavaScript script that generates an array of 1000 random numbers and defines three functions:
doRedeuce(pArray)
: Uses the reduce()
method to sum the elements of the array.doLoop(pArray)
: Iterates through the array using a traditional for loop, adding each element to a running total.doForEach(pArray)
: Iterates through the array using the forEach()
method, adding each element to a running total.The script then assigns the results of each function to variables named after their corresponding names (redeuceResult
, loopResult
, and forEachResult
).
HTML Preparation Code
Since there is no HTML preparation code provided, we can assume that this step is skipped or not applicable for this specific benchmark.
Individual Test Cases
The individual test cases are defined as an array of objects, each representing a single test. Each object contains two properties:
Benchmark Definition
: A string representing the JavaScript code that performs the actual benchmarking. This code calls one of the three functions (reduced(), doLoop(arrRandom), or forEach(arrRandom)) and assigns the result to a variable with the same name.Test Name
: A string representing the name of the test case.The provided individual test cases are:
reduce
for loop
forEach
Latest Benchmark Result
The latest benchmark result is an array of objects, each containing information about a single execution. The relevant properties include:
Browser
: The name and version of the browser being tested.DevicePlatform
: The type of device platform (e.g., Desktop).OperatingSystem
: The operating system being tested.ExecutionsPerSecond
: The number of executions performed per second.The provided benchmark result contains data for each test case:
for loop
forEach
reduce
Options Comparison
Let's examine the three options compared in this benchmark:
This approach uses the reduce()
method to sum the elements of the array. The reduce()
method takes two arguments: an initial value (usually set to 0) and a callback function that combines the current value with each subsequent element.
Pros:
Cons:
This approach uses a traditional for loop to iterate through the array, adding each element to a running total.
Pros:
Cons:
This approach uses the forEach()
method to iterate through the array, adding each element to a running total.
Pros:
Cons:
Other Considerations
When evaluating the performance of these options, it's essential to consider factors such as: