Script Preparation code:
x
 
// Create an array of 1000 random intergers between 1 and 10000
var arrRandom = [];
for(var intCtr=0; intCtr<1000; intCtr++) {
  arrRandom.push(Math.floor(Math.random() * Math.floor(10000)));
}
function reduceCallback(accum, curr, index) {
    accum[curr] = index;
    return accum;
}
function doRedeuce(pArray) {
    return pArray.reduce(reduceCallback, {});
}
function doLoop(pArray) {
    var accum = {};
    for(var index=0; index<pArray.length; index++) {
        accum[pArray[index]] = index;
    }
    return accum;
}
function decrWhile(pArray) {
    var accum = {};
    var index = pArray.length;
    while(index--) {
        accum[pArray[index]] = index;
    }
    return accum;
}
function doForEach(pArray) {
    var accum = {};
    pArray.forEach(function(item, index) {
        accum[item] = index;
    });
  return accum;
}
Tests:
  • reduce

     
    var redeuceResult = doRedeuce(arrRandom);
  • for loop

     
    var loopResult = doLoop(arrRandom);
  • forEach

     
    var forEachResult = doForEach(arrRandom);
  • decrement while

     
    var decrWhileResult=0;
    decrWhileResult = decrWhile(arrRandom);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    reduce
    for loop
    forEach
    decrement while

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 5 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
Chrome 74 on Windows
View result in a separate tab
Test name Executions per second
reduce 1761.5 Ops/sec
for loop 1520.3 Ops/sec
forEach 1867.2 Ops/sec
decrement while 2142.6 Ops/sec