Always return on events is faster, but why?
jsperf.com
jsperf.com
One of the most important things to remember while using jsPerf is that setup phase can and will be executed multiple times. As the result the list of listeners attached to the DOM node is growing and this in turn slows down the event dispatch.
"No return" case is run second so the list of listeners is already large and thus it is slower than "return case".
You should either unregister listeners in tear down phase or register them only once at global initialization time. Here is the fixed variant:
http://jsperf.com/always-return-on-jquery-events/28
[also from the JavaScript VM point of view function () { } and function () { return; } are completely the same]
If the OP wanted to measure function with or without return , even without realizing how futile that is, they still should not include things like jQuery.
And the web being as it is, testing with jQuery might even be more useful than testing without it.
You should read http://zedshaw.com/essays/programmer_stats.html
What I meant was that if I'm going to run a website with jQuery calls, I should absolutely test with jQuery calls. It makes no sense to claim that one should, say, always use short variable names, if one then ships with google closure.
Most people that are hellbanned on HN have not at all deserved it. Ignoring spambots, I've seen perhaps two users that got a hellban for consistently problematic posts rather than a single post annoying someone. And one of those has actual mental issues.
And the answer to it being overused is for people to call it out whenever a user is trying to contribute but hellbanned, and we don't agree with the ban after taking a look at their comment history.
Looks like their last comment sucked, and it's easier for a mod to ban someone than to reply, “that comment sucked” and hope they get better. Mods are never perfect, they have a big job, and people can (successfully) appeal hellbans.
Someone named eulerphi replied to your comment, but they're "hellbanned" from Hacker News and their reply is only visible if you turn on showdead in your settings.
The only thing I can imagine taking longer would be the compilation step itself... but I had always assumed jsPerf didn't work like this, i.e. I thought it would wrap the test code in a 'for' loop and then eval the whole loop, rather than doing the eval call inside the loop.
It's the same thing if you compare the selectors $(this) and $((((this))));
I don't know why, but the problem must come from jsperf. Thats why when I use this tool I always run it at least 5 times to be sure.
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?
https://github.com/jquery/jquery/blob/a5037cb9e3851b171b49f6...
So returning undefined does NOT stop propagation.
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.
I posted the jQuery code in a previous comment; take a look at that and you can see how it works.
Neither does jQuery interpret a return of undefined as a stopPropagation, as this fiddle shows:
Both versions have the same return value as illustrated by this fiddle http://jsfiddle.net/QHxJ3/
No, it doesn't. jQuery uses a strict test for false and does not stop propagation for other falsy return values from an event listener.
The documentation could be more clear on this point. All it says is: "Returning false from an event handler will automatically call event.stopPropagation() and event.preventDefault()."
http://api.jquery.com/on/#event-handler
Here's the code that does this check:
if ( ret !== undefined ) {
if ( (event.result = ret) === false ) {
event.preventDefault();
event.stopPropagation();
}
}
https://github.com/jquery/jquery/blob/master/src/event.js#L3...Reading that code, it almost seems redundant at first to have a !== undefined check when the === false is already a strict comparison. But there is that assignment hidden inside the if expression. So the code is really the same as this more clearly written version:
if ( ret !== undefined ) {
event.result = ret;
if ( ret === false ) {
event.preventDefault();
event.stopPropagation();
}
}
This would also have the same effect: if ( ret !== undefined ) {
event.result = ret;
}
if ( ret === false ) {
event.preventDefault();
event.stopPropagation();
}
These all do the same thing: set event.result only if ret is not undefined, and then call preventDefault and stopPropagation only if ret is false (and not just a falsy value).Consider this example:
function onePlusOne() { return 1 + 1; }
function two() { return 2; }
alert( onePlusOne === two ); // false, not the same function
alert( onePlusOne() === two() ); // true, same value