Use what works for you, and don't bother with premature optimization.
By the way it's not greatest bind use case, I wouldn't use bind here. In
some older versions of node (v0.6.3 ?) , setTimeout passes one argument to
the callback, and in your example it will be logged as well.
On Tuesday,
For me
setTimeout(function() { console.log(x) }, 5000)
is cleaner than
setTimeout(console.log.bind(console, x), 5000)
I used .bind() a lot to get rid of scope-problems for once and all
times. But then the code became, well, a mess: Everything I saw was
calls to .bind() ;-) Way too much
I don't think this part of JavaScript will ever change. Node does not
modify the JavaScript language it uses.
Lua does this though if you want to try luvit.io (node ported to lua). It
has syntax sugar to make defining and calling context-first methods easier.
-- This is lua code, not JS
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
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
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
On Tue, May 29, 2012 at 2:55 PM, Marco Rogers marco.rog...@gmail.comwrote:
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.
Especially don't assume that you're
Also, there have been situations where V8's advantage in one technique over
another has reversed.
On Tue, May 29, 2012 at 11:59 AM, Scott González
scott.gonza...@gmail.comwrote:
On Tue, May 29, 2012 at 2:55 PM, Marco Rogers marco.rog...@gmail.comwrote:
Wow, I remember bind being much slower
On May 28, 8:52 pm, phidelta philipp.dun...@gmail.com wrote:
The question I have is: what is more performant? Is there any reason
to avoid .bind()?
IIRC there is a performance hit when using bind, but whether it will
seriously impact your application is something you'll have to test.
--
Job