Thing is the smaller code IS more complex because it gets callbacks involved. It's more functions inside the loop.

On top of being cryptic.

Hence this, whilst tiny:

let areAllFourLegs = pets.every(p => p.nLegs === 4);

Would actually be far slower executing than:

function check(pets) {
for (var pet of pets) if (pet.nLegs != 4) return false;
return true;
}
let areAllFourLegs = check(pets);

Why? Because it's only one function call instead of five! Or more specifically, 1 + (r | n)

Every time a function is called, you introduce a state push, a far call, garbage collection, state pop…

Think about it, whilst writing less code, you’ve introduced extra function calls (and returns) INSIDE the loop!

You MIGHT get away with it in Chrome since they’ve been optimizing for shoddy code whilst neglecting what already was, but FF? Not so much.

Shame JSPerf went the way of the dodo (thanks almost entirely to dependency hell)… Probably too many of us were using it to debunk claims about how much “better” certain hot and trendy BS is.

Laugh is, from an execution standpoint, for/of in this case would be one of the slower loops… since it’s all objects one could use:

for (var i = 0, pet; pet = pets[i]; i++) {

Which is — strange as it looks — the fastest (executing) way to iterate through an array or array-like of objects. Like a nodeList.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store