Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for The Price of Currying
Anders
Anders

Posted on

     

The Price of Currying

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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
wilsongramer profile image
Wilson Gramer
  • Location
    Massachusetts
  • Joined

Theadd example is pretty surprising! I wonder if the interpreter is inlining the uncurried version, so it's not actually doing any function calls.

CollapseExpand
 
anders profile image
Anders
  • Location
    Sweden
  • Work
    CTO at Upstream Business Solutions
  • Joined

Something is definitely going on there to make it such a huge difference. It would be interesting to see a breakdown of what actually happens in the JIT compiler and what the actual executing code ends up being.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Sweden
  • Work
    CTO at Upstream Business Solutions
  • Joined

More fromAnders

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp