Currying in JavaScript
Function currying is way to use same arguments in the sequence of functions.
function someFunction(radius,numberOfCircles){
const area = Math.PI * radius * radius
return function (){
const totalArea = area * numberOfCircles
return totalArea
}
}const heavyFunctionProcessed = someFunction(5)const totalAreaOF3Circles = heavyFunctionProcessed(3)
const totalAreaOF4Circles = heavyFunctionProcessed(4)
...
Above is the example of function currying in javascript.
someFunction
is returning another (anonymous ) function and we are using that on other places.
Benefit
We have called heavyFunctionProcessed
2 times but the calculations for area was done only once. So this way cpu took less processing and our code is effecient.
Drawback
As we saw area value was processed once and then Javascript store its value for future. This way we are not repeating the same calculations but we are occupying space in memory. So for this reason currying functions can take more space.
If you get to learn something new then smash the 👏 button 😉.