JavaScript Method Chaining… It’s All So STUPID!

Jason Knight
CodeX
Published in
9 min readAug 21, 2021

--

For some time we’ve seen the rise of chaining methods and responses together. jQuery started the trend, and now with things like promises it’s risen to new heights of popularity… but to be brutally frank, it’s all such mind-numbingly overcomplicated nonsense!

Garbage that’s only further compromised code clarity and increased overhead thanks to making damned near everything “callback’ driven. Callbacks introduce overhead just in terms of their being functions, particularly when used inside a loop. Worse if they’re anonymous functions — even arrow driven — they’re reparsed on every usage!

Take this article here:

Good writing style, excellent overview of how it works, and I hate to rip this guy to shreds… but what it’s doing is really REALLY dumb because of how bloated, slow, and inefficient the resultant code is.

He’s got this:

const csStudents = students.filter(student => student.class === 'CS')
.map(student => student.email)
.forEach(email => sendInvitation(email));

Doing the job of:

for (let student of students) {
if (student.class === "CS") sendInvitation(student.email);
}

I don’t know about you, but I find the latter a hell of a lot easier to deal with. The real laugh being that his csStudents variable wouldn’t even hold the array, it would hold the result of the forEach. News flash, Array.forEach returns undefined. ALWAYS! As such there’s no reason for that variable to exist either.

And in a way it plays to the LIE a lot of people are saying right now of “don’t use for loops” as if these Array methods like .map and .filter don’t internally do the exact same blasted thing!

Worse, all of those array methods like map and filter create NEW arrays, so they have to allocate the memory for those arrays, and garbage collection has to de-allocate that memory when done, and they have to be passed as a response! Each of those array functions loop the array…

--

--

Jason Knight
CodeX
Writer for

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

Recommended from Medium

Lists