Wow, I remember bind being much slower than that a while back. Maybe some
work has been done. Either way it proves the point. Don't assume you're
better at optimizing js than v8 is.
:Marco
On Tuesday, May 29, 2012 10:46:24 AM UTC-7, Isaac Schlueter wrote:
>
> fn.bind() is a bit slower than calling function () { fn() }, it's
> true. But it's a difference between 5,000,000Hz and 10,000,000Hz. If
> you're doing IO anywhere, an extra µs isn't going to affect
> performance noticeably.
>
> Write your program so that it's clear and readable. Then profile it.
> Then make the silly micro-optimizations only where they matter.
>
> On Tue, May 29, 2012 at 10:37 AM, Marco Rogers <[email protected]>
> wrote:
> > I believe the current state of things is that the closure method of
> binding
> > is optimized and very fast in v8. Function#bind is not. This is partly
> due
> > to the fact that nobody uses it, so it may not be worth the time to
> > optimize. But I agree with those that say do what works for you. Deal
> with
> > performance when it becomes a problem. And don't try to second guess v8.
> > It's probably smarter than you :)
> >
> > One thing you could do, that think is more for code clarity than
> > performance, is pre-bind any important objects.
> >
> > Object.keys(console).forEach(function(key) {
> > if(typeof console[key] === 'function') {
> > console[key] = console[key].bind(console);
> > }
> > });
> >
> > Now you can just pass console.log anywhere and it will always be
> properly
> > bound. Keep in mind that this hampers your ability to do call/apply, so
> use
> > it sparingly. It also doesn't actually help your setTimeout example
> because
> > you need to pass a value, YMMV :)
> >
> > :Marco
> >
> >
> > On Monday, May 28, 2012 5:52:46 PM UTC-7, phidelta wrote:
> >>
> >> Hi all,
> >>
> >> I have recently come to use .bind a lot, because it makes for very
> >> clean code.
> >>
> >> So rather than doing
> >>
> >> <code>
> >> var x; // this has some value that comes from prior code
> >> setTimeout(function() { console.log(x) }, 5000);
> >> </code>
> >>
> >> I am now often doing
> >>
> >> <code>
> >> var x;
> >> setTimeout(console.log.bind(console, x), 5000);
> >> </code>
> >>
> >> The question I have is: what is more performant? Is there any reason
> >> to avoid .bind()?
> >>
> >> Regards, Phil
> >
> > --
> > Job Board: http://jobs.nodejs.org/
> > Posting guidelines:
> > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> > You received this message because you are subscribed to the Google
> > Groups "nodejs" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> > http://groups.google.com/group/nodejs?hl=en?hl=en
>
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en