Jason Knight
2 min readOct 11, 2020

Good article, but one thing to watch out for is the overhead of excess function calls. It's one of the things that people who try to use "functional programming" in JavaScript often don't understand.

EVERY function call has excess overhead, and it's why for loops often execute many times faster than trash like Array.every / Array.each.

Take your doubler, in practice the object version ends up making two function calls instead of one, requires property lookups, and adds a bunch of overhead that could in fact make it many times slower than the switch/case version preceding it.

Just like how that derpy -- but hot and trendy -- daisy-chaining of Array methods for the string doubler is likely many, MANY times slower than just building and returning a new string with a normal loop... and is far less clear in explaining what it's doing... and consumes far, FAR more memory given the number of array copies being built.

This:

for (
var i = 0, result = '', ch;
ch = input.charAt(i);
i++
) result += ch + ch;
return result;

In all likelihood would execute faster, and makes it FAR clearer what the code is actually doing. And yes, I USED VAR! 1) for a reason that should be obvious, and 2) so that we aren’t introducing extra overhead we don’t actually need for the dimwitted const/let garbage! I call them dimwitted as I’ve never in 40 years of programming found block level scoping beneficial, particularly weighed against the overhead of their implementation! If function level scope is “too hard” or “not enough” for you folks, please just back the **** away from the keyboard!

Sadly that describes a LOT of the newer scripting stuff. Poor performance, convoluted and often cryptic syntax, pissing on stack handling and garbage collection, and not even providing any code clarity benefits either.

Laughably, for/of might even be slower than the conventional for. Shame JSPerf went the way of the dodo thanks to dependency hell.

The syntax would be so much cleaner.

var result = '';
for (var ch of input) result += ch + ch;
return result;

Sadly I’m seeing far too much dicking around with array functions and callbacks for things that should NOT be getting that extra overhead involved. It’s getting REALLY mind-blowingly stupid the asshat hoops people will now jump through as if “teh for loops r teh evilz!!!”. Seriously, some folks out there now see a “for” loop and they lose their huffing minds like you just took a dump in their corn flakes. Whiskey Tango Foxtrot!?!

Jason Knight

Accessibility and Efficiency Consultant, Web Developer, Musician, and just general pain in the arse