Live data from Hacker News

Always return on events is faster, but why?

jsperf.com

1–10 of 52 posts

Re: Always return on events is faster, but why?

#2
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?

#3

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?

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?

#6
post #4

Doesn't a return without argument return false and thus have the same effect as preventDefault()? That would explain the difference, since the events are not "bubbling up".

return without return false is just like breaking out of a method i think.. The events would still bubble since false isn't returned

Re: Always return on events is faster, but why?

#7

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?

No. It equates to returning undefined, which is the same as having no return statement at all.

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?

#9

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?

No. It equates to returning undefined, which is the same as having no return statement at all.

!!undefined === false

Re: Always return on events is faster, but why?

#10
post #4

Doesn't a return without argument return false and thus have the same effect as preventDefault()? That would explain the difference, since the events are not "bubbling up".

I think `function(){}` and `function(){return;}` are equivalent, at least functionally... Still can't explain the time difference, though.
Post reply on HN