_.each vs .forEach

Benchmark created on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.6/underscore-min.js"></script>

Setup

const orders = Array.from({ length: 1000 }, (_, i) => ({
  id: i,
  amount: Math.floor(Math.random() * 1000) + 10,
  status: ['pending', 'processing', 'completed', 'cancelled'][Math.floor(Math.random() * 4)],
  items: Math.floor(Math.random() * 5) + 1
}));


let totalRevenue = 0;
let orderCounts = { pending: 0, processing: 0, completed: 0, cancelled: 0 };


const processOrder = (order) => {
  if (order.status === 'completed') {
    totalRevenue += order.amount;
  }
  orderCounts[order.status]++;
};

Teardown

totalRevenue = 0;
orderCounts = { pending: 0, processing: 0, completed: 0, cancelled: 0 };

Test runner

Ready to run.

Testing in
TestOps/sec
_.each
_.each(orders, processOrder);
ready
.forEach
orders.forEach(processOrder);
ready

Revisions

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