Functional vs For Of Array methods

JavaScript performance comparison

Test case created by asleepysamurai

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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)]
      });
  }
  

};

Benchmark.prototype.teardown = function() {
  totalPaidExpensesInINR = null;

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/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);
pending…
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;
    }
}
pending…

Compare results of other browsers

Revisions

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

0 Comments