JSFiddle - React, Tailwind, and code Playground

by Anton Spirin

JavaScript

var arr = new Array(1000000).fill({ categoryid: 3 });

var t1_0 = performance.now();
var result = arr.reduce(function(catSum, current) {
  return catSum + (current.categoryid === 3 ? 1 : 0);
}, 0);
var t1_1 = performance.now();

var t2_0 = performance.now();
var result2 = arr.filter(n => n.categoryid === 3).length;
var t2_1 = performance.now();


alert(`
  reduce: ${t1_1 - t1_0}ms
  filter: ${t2_1 - t2_0}ms
  result reduce: ${result}
  result filter: ${result2}
`);