Is using several '.filter' calls on a big array bad for performance?

Viewed 2584

I wrote this piece of code to filter an array of words. I wrote a filter function for every type of word I want to filter out and apply them sequentially to the array:

  const wordArray = rawArray.filter(removeNonDomainWords)
                            .filter(removeWordsWithDigits)
                            .filter(removeWordsWithInsideNonWordChars)
                            .filter(removeEmptyWords)
                            .filter(removeSearchTerm, term)
                            .map(word => replaceNonWordCharsFromStartAndEnd(word))

This code iterates over the whole array six times if I am not mistaken.

Wouldn't it be more efficient to write one (more complex, yet still easy in my scenario) filter function that logically combines the filter functions to achieve the same result?

I learned about filter in the context of Functional Programming which is supposed to make my code shorter and faster. That's why I probably didn't question what I was writing, thinking 'I am doing FP, this gotta be good'.

Thanks!

2 Answers
Related