Functional vs For Of Array methods

Benchmark created by asleepysamurai on


Setup

tripExpenses10000 = [];
  
  while(tripExpenses10000.length < 10000){
      tripExpenses10000.push({
          amount: (Math.random() * (250) + 1),
          currency: ['INR', 'USD'][Math.round(Math.random() % 2)],
          paid: [false, true][Math.round(Math.random() % 2)]
      });
  }

Teardown



            totalPaidExpensesInINR = null;
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
10,000 Elements Functional
let totalPaidExpensesInINR = tripExpenses10000
    .filter(expense => expense.paid)
    .map(expense => {
        if(expense.currency == 'USD')
            return expense.amount * 70;
        else
            return expense.amount;
    })
    .reduce((amountA, amountB) => amountA + amountB);
ready
10,000 Elements For Of
let totalPaidExpensesInINR = 0;

for(let expense of tripExpenses10000){
    if(expense.paid){
        if(expense.currency == 'USD')
            totalPaidExpensesInINR += (expense.amount * 70);
        else
            totalPaidExpensesInINR += expense.amount;
    }
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by asleepysamurai on