Is using several '.filter' calls on a big array bad for performance in Javascript? -
i wrote piece of code filter array of words. wrote filter function every type of word want filter out , apply them sequentially array:
const wordarray = rawarray.filter(removenondomainwords) .filter(removewordswithdigits) .filter(removewordswithinsidenonwordchars) .filter(removeemptywords) .filter(removesearchterm, term) .map(word => replacenonwordcharsfromstartandend(word)) this code iterates on whole array 6 times if not mistaken.
wouldn't more efficient write 1 (more complex, yet still easy in scenario) filter function logically combines filter functions achieve same result?
i learned filter in context of functional programming supposed make code shorter , faster. that's why didn't question writing, thinking 'i doing fp, gotta good'.
thanks!
well, does iterate 6 times, not on whole initial array. each time filtered becomes smaller. more effective have 1 filter method, difference might not great expect.
if still want use solution, can increase performance using selective (that filter expected filter out most) first. way, following arrays smaller , there less iterate through.
as @redu points out (in comments) can chain filters using || operator. make sure 1 iteration.
the reason behind array.prototype.filter returns new array. compare java stream api, returns stream, , can go "depth first" through call list. down side of need terminal operation in end, "collect" result.
in javascript
rawarray.filter(x) iterates rawarray , returns new filtered array - can in turn filtered, or used is. result in call x each of elements in rawarray.
in java equivalent be
rawarray.stream().filter(x) which not @ @ point. no calls x done. return value stream, can used later. can further filtered, not until values collected in way - terminal operation - calls made.
lets compare javascript
rawarray.filter(x).filter(y).length to java
rawarray.stream().filter(x).filter(y).count() in javascript, first iterate on of elements of rawarray, calling x each of them, , store result in intermediate array. javascript engine iterate on of elements of intermediate array, calling y each element, , store result in second intermediate array, check size of.
in java, snippet result in vm iterating on elements of rawarray, first calling x, and, if x true, calling y on each element, and, if still true incrementing counter. there no intermediate arrays, , 1 iteration on dataset.
functional programming interesting, , when used properly, creates less code less complex , ideally perhaps bit easier read, hand on lot of responsibility framework (or engine or vm or whatever), , important realize seemingly similar code, while behaving similarly, can perform vastly differently in different environments.
Comments
Post a Comment