Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slicing arguments causes huge performance drop in _.bind() #2197

Closed
unwitting opened this issue May 27, 2015 · 5 comments
Closed

Slicing arguments causes huge performance drop in _.bind() #2197

unwitting opened this issue May 27, 2015 · 5 comments

Comments

@unwitting
Copy link

Throughout, I'm referring to: http://jsperf.com/native-bind-vs-underscore

We've been noticing that _.bind (via _.bindAll) is hugely slowing down our code, and I looked into exactly what it is about _.bind that causes the slowdown.

The jsperf runs everything from native bind up to what _.bind does, with more and more of the various safety checks in between (this is assuming we never have to move on to the second line of _.bind ie that all is as expected in a sane environment). Each of these slightly (negligibly) slows down execution until moving from just referencing ctx directly instead of slice.call(arguments, 1), which halves the performance of the function in one blow.

What's the justification for the slice?

@unwitting
Copy link
Author

This might be obsoleted if #2182 goes ahead since lodash.bind is a whopping 2 times faster than native (╯°□°)╯︵ ┻━┻ http://jsperf.com/native-bind-vs-lodash

@unwitting
Copy link
Author

Note: a suggestion would be to offer a different call for the case in which the caller wants to provide extra arguments to bind with, since the common use case (I assume) is _.bind(fn, this), not _.bind(fn, this, other, stuff) and the safety for extra args causes such a massive perf hit.

@ZeikJT
Copy link

ZeikJT commented May 27, 2015

The slice.call being run on arguments means you're leaking arguments
https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#32-leaking-arguments

The recommended fix is to do a for loop inline and create a new array based on arguments.

@jridgewell
Copy link
Collaborator

We can get the same speedups by dropping nativeBind.

@jdalton
Copy link
Contributor

jdalton commented Jun 1, 2015

Closed by #2199.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants