Script Preparation code:
AخA
 
var arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5, 2, 2, 2, 2, 2, 9, 4, 5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
Tests:
  • Using an object to hold the counts

     
    const counts = {};
    for (const num of arr) {
      counts[num] = counts[num] ? counts[num] + 1 : 1;
    }
  • Using reduce

     
    arr.reduce(function (acc, curr) {
      return acc[curr] ? ++acc[curr] : acc[curr] = 1, acc
    }, {});
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Using an object to hold the counts
    Using reduce

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36
Chrome 113 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
Using an object to hold the counts 1863877.0 Ops/sec
Using reduce 3042336.2 Ops/sec