Search code examples
javascriptfunctional-programming

functional programming efficiency vs imperative


I'm new to functional programming and I just ran into something and was wondering if there was a way around this.

Suppose I have

myArray = [
  { a : 1 }
  { a : 4 }
  { a : 5 }
  { a : 6 }
  { a : 7 }
  { a : 8 }
]

Let say I need to do statistical operations on this data set such as

const median = myArray[ Math.ceil( myArray.length / 2 ) ]['a'] // Math.ceil .. Side Effect?
const fiveOrMore = myArray.filter( value => value.a >= 5 )
const lessThanFive = myArray.filter( value => value.a < 5 )

Some arbitrary examples. The problem with this as of right now is that with increasing amount of statistical operations I need to do, the efficiency decreases.

With imperative style, I could do everything in ONE for loop. Is this the wrong approach to functional programming that I am taking or is it a trade off of functional programming paradigm itself?


Solution

  • This is of course less performant. And the performance hit is a trade off of the style you chose.

    One may argue that it's not a big deal since the time complexity is O(n), the only difference is the constant. I'd say make sure you preformance-test your app. If it's slow -- it's time to optimize certain blocks of code.

    Premature optimization is evil. There will be many cases where imperative code works faster or much faster of muchly much faster than the functional one, and depending on the case you may or may not be okay with that.

    Also, there are various techniques to improve the performance. You don't necessarily want to change the style. Say, in some cases memoization can drastically boost the speed of the function, while keeping the code functional. Just think outside the box.