HTML Preparation code:
AخA
 
1
<!--your preparation HTML code goes here-->
Script Preparation code:
x
 
function sumWithReduce(arr) {
    return arr.reduce((x, y) => (y = +y) ? x + y: x, 0)
}
function sumWithReduceInt16(arr) {
    return arr.reduce((x, y) => x + y, 0)
}
function sumWithLoop(arr) {
    let length = arr.length;
    let init = 0;
    let val;
    
    while (length--)
        if (val = +arr[length])
            init += val;
    
    return init; 
}
function sumWithLoopInt16(arr) {
    let length = arr.length;
    let init = 0;
    let val;
    
    while (length--)
        init += val;
    
    return init; 
}
const nums = Array.from({length: 10000}, () => Math.floor(Math.random() * 65536))
// throw in some non numbers
nums[4] = '5'; nums[22] = NaN;
// coerces non numbers to zero
const numsInt = new Uint16Array(nums);
Tests:
  • Sum with 16-bit array and loop

     
    sumWithLoopInt16(numsInt)
  • Sum with standard array and reduce

     
    sumWithReduce(nums)
  • Sum with standard array and loop

     
    sumWithLoop(nums)
  • Sum with 16-bit array and reduce

     
    sumWithReduceInt16(numsInt)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Sum with 16-bit array and loop
    Sum with standard array and reduce
    Sum with standard array and loop
    Sum with 16-bit array and reduce

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 5 months ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0
Firefox 133 on Windows
View result in a separate tab
Test name Executions per second
Sum with 16-bit array and loop 87659.8 Ops/sec
Sum with standard array and reduce 12308.9 Ops/sec
Sum with standard array and loop 16043.6 Ops/sec
Sum with 16-bit array and reduce 6839.9 Ops/sec