DEV Community

Discussion on: An Adequate Introduction to Functional Programming

Collapse
 
jeromedeleon profile image
Jerome De Leon

This is great article but if you care about performance,
Using this approach is much more appropriate because you only loop once
and not creating new reference for every method you use.

for (let i = 0; i < numbers.length; i++) {
  if (numbers[i] % 2 === 0) {
    if (numbers[i] < 5) {
      result.push(5)
      continue
    }
    result.push(numbers[i])
  }
}
Enter fullscreen mode Exit fullscreen mode

Unlike this one, although much more readable, but every function creates its own loop.

numbers
  .filter(n => n % 2 === 0)
  .map(n => n < 5 ? 5 : n)
Enter fullscreen mode Exit fullscreen mode

Correct me if I'm wrong here 😁.
It's just that I prefer performance rather than readability.

Collapse
 
mr_bertoli profile image
Andrea Bertoli

Thanks for this comment 😁, but I totally disagree with you.

Thinking about performance before profiling you code is called "premature optimization" which is a known to be a bad thing in software development, also because without appropriate tests, you don't even know where the "bottlenecks" really are.

Furthermore, 90% of the time you don't have performance problems nor this type of operations leads to performance issues (unless you work with long long long arrays). Also, often performance problems are present due to bad software architecture.

Finally, if you want really performant code maybe you need to consider other solutions than JS 😅

Collapse
 
jeromedeleon profile image
Jerome De Leon

Thanks. I'd look into that.