I stumbled uponthis very interesting article on the concept of currying and how to it in practice, I definitely recommend you read it =).
As I am very interested in the performance cost of how we program, I had to explore the impact of using this method/feature or whatever we want to call it, here are my findings.
Test 1 (add)
Curried Version (from the referenced article, using lodash)
functionadd(a,b,c){returna+b+c;}constcurryAdd=_.curry(add);curryAdd(1)(2)(3);
Basic JS Version
functionadd(a,b,c){returna+b+c;}add(1,2,3);
And the results are just horrible. Using the curried version here is 99.96% slower, its so slow its almost unfathomable.
Test 2 (isType)
Base Items List for this test (from the referenced article)
constitems=[{name:"Mango",type:"Fruit"},{name:"Tomato",type:"Vegetable"},{name:"Strawberry",type:"Fruit"},{name:"Potato",type:"Vegetable"},{name:"Turnip",type:"Vegetable"},{name:"Banana",type:"Fruit"},{name:"Carrot",type:"Vegetable"},];
Curried Version (from the referenced article)
constisType=obj=>type=>obj.type===type;constisFruit=item=>isType(item)("Fruit");constisVegetable=item=>isType(item)("Vegetable");constfruits=items.filter(isFruit);constvegetables=items.filter(isVegetable);
Basic JS Version
functionisFruit(obj){return(obj.type=='Fruit');}functionisVegetable(obj){return(obj.type=='Vegetable');}constfruits=items.filter(isFruit);constvegetables=items.filter(isVegetable);
Performance here is IDENTICAL for both versions which is nice, and expected, since you end up with basically the same thing for .filter to do.
Wrap Up
As is illustrated in the referenced article there are certainly use cases for currying, but do use it with restraint, especially if your code is executed by many many people, or many many times.
--
All benchmarking done withhttps://jsbench.me/ in the latest version of Chrome on a Windows PC. Actually declaring the functions was NOT included in the benchmarked code, that happens in the setup stage. That is slower in the curried case for both tests above.
Top comments(2)
For further actions, you may consider blocking this person and/orreporting abuse