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