News for hackers. Stuff that matters.
Re:Always return on events is faster, but why? by dominicglenn on Friday October 18, 2013 @05:49AM (#6570529)
I posted it on the page as well:

Doesn't just calling return from an event equate to returning boolean false, which means you are invoking the effects of preventDefault() and stopImmediatePropagation() thus explaining why with return it's faster as the event stops bubbling immediately?

Re:Always return on events is faster, but why? by al2o3cr on Friday October 18, 2013 @09:10AM (#6571137)
FWIW, the underlying jQuery code explicitly tests for false with ===:

https://github.com/jquery/jquery/blob/a5037cb9e3851b171b49f6...

So returning undefined does NOT stop propagation.

Re:Always return on events is faster, but why? by EmielMols on Friday October 18, 2013 @05:51AM (#6570532)
No. It equates to returning undefined, which is the same as having no return statement at all.
Re:Always return on events is faster, but why? by dreen on Friday October 18, 2013 @05:53AM (#6570544)
Although it obviously isn't quite the same. I wonder what is the test result for no return vs return true
Re:Always return on events is faster, but why? by Tarang on Friday October 18, 2013 @06:00AM (#6570564)
Still faster, check revision 6 http://jsperf.com/always-return-on-jquery-events/6

revision 7 http://jsperf.com/always-return-on-jquery-events/7. Just return vs return true (faster).

But as smilekzs points out revision 3 this might not be about the return statement.

Re:Always return on events is faster, but why? by lucideer on Friday October 18, 2013 @05:59AM (#6570558)
!!undefined === false
Re:Always return on events is faster, but why? by bengillies on Friday October 18, 2013 @06:03AM (#6570580)
right. undefined is falsy, but it's quite different to false (i.e. undefined !== false)
Re:Always return on events is faster, but why? by lucideer on Friday October 18, 2013 @07:00AM (#6570718)
true, but a boolean return value is expected for event listener callbacks.
Re:Always return on events is faster, but why? by Stratoscope on Friday October 18, 2013 @12:54PM (#6572398)
That's not quite how it works. jQuery checks specifically for false with an === comparison. It doesn't do anything like a !! on the return value to convert it to boolean true or false. It only calls stopPropagation and preventDefault when the return value is false, not just any falsy value.

I posted the jQuery code in a previous comment; take a look at that and you can see how it works.