I am looking at improving some of our for each loops and created a test benchmark to compare different ways we are using them. The test can be found here.
I was surprised to find out that this code:
function add(val) {
sum += val;
}
values.forEach(add);
performs better than this one.
values.forEach(function(val) {
sum += val;
});
Aren't these exactly the same? What makes the first code snippet faster than the second one?