I know that the Array prototype method
filteris generally preferred over forEach
What makes you say that?
forEach are two different things; they are not interchangeable. There are situations where you’ll use
filter, and the ones where you’ll use
I believe it is typically faster at the job as well
Don’t believe something is faster. Measure it; this way you’ll know.
So which should be expected to be faster? I imagine there may be […]
Again, don’t imagine. Performance is not about beliefs and what someone imagines. It’s about exact measurements and hard data. You have your both approaches, and that’s great. Now, you just have to run them side by side with the real data, and see which one outperforms the other.
Note the importance to use the real data. If the algorithm is expected to be used for the arrays containing about a dozen elements, don’t compare the measurements using the arrays containing a billion elements. Similarly, if you want to run the algorithm on an array containing billions of elements, don’t take measurements with an array containing a dozen elements, as the performance comparison would be perfectly irrelevant for your actual case.