Bug Tracker

Ticket #2655 (closed enhancement: fixed)

Opened 5 months ago

Last modified 5 months ago

jQuery.event module optimizations

Reported by: flesler Assigned to: anonymous
Type: enhancement Priority: minor
Milestone: 1.2.4 Component: event
Version: 1.2.3 Keywords: event handle fix
Cc: Needs: Review

Description (last modified by brandon) (diff)

Hi, here are a few modifications, that I think they will improve perfomance, and some make the code shorter, I'll explain them in case the reason for any change is not clear.

jQuery.event.fix

  • The same event object, can't be fixed more than once (it happens when triggering.

jQuery.event.handle

  • Removed the {} fallback when calling jQuery.event.fix, event.type is used in the next line, so IF you were to fall on the last case, the next line would alert 'event.type is not defined' seems like the 3rd case can't be reached.
  • Avoided the need for Array.prototype.slice.call( arguments, 1 ) and args.unshift().
  • Replaced args[0] for event when looping through the handlers, those were 2 array lookups per handler.
  • Same for the namespace (parts).
  • Simplified the val/ret part, ret is not needed and the code is now shorter.
  • The forced call to preventDefault and stopPropagation is only checked and done once, after the loop.

Attachments

handle-fix.diff (2.1 kB) - added by flesler 5 months ago.
Changes
handle-fix.2.diff (3.1 kB) - added by flesler 5 months ago.
2nd version
handle-fix.3.diff (3.2 kB) - added by flesler 5 months ago.
3rd Version

Change History

Changed 5 months ago by flesler

Changes

Changed 5 months ago by flesler

2nd version

Changed 5 months ago by flesler

Merged the first diff with 2 more changes.

jQuery.event.handle

* the copy of data and the handler to the event object, is only done if the handler is really going to be called, small change but it's correct and %0.001 faster. * I saw the new function that's gets bound and saved into $.data('handle'), I simplified it a bit, it was kinda redundant :)

Changed 5 months ago by flesler

3rd Version

Changed 5 months ago by flesler

The third version adds 2 things.

* It caches the originally: !parts[0] && !event.exclusive in a variable. Those were 2 boolean casting and 2 boolean checks on each iteration.

* Instead of calling twice jQuery.data, an empty hash if used. It shorter and should be faster.

Changed 5 months ago by flesler

ugh.. forget the typos in the previous post.. I'm still sleepy.

Changed 5 months ago by brandon

  • description changed from Hi, here are a few modifications, that I think they will improve perfomance, and some make the code shorter, I'll explain them in case the reason for any change is not clear. jQuery.event.fix * The same event object, can't be fixed more than once (it happens when triggering. jQuery.event.handle * Removed the {} fallback when calling jQuery.event.fix, event.type is used in the next line, so IF you were to fall on the last case, the next line would alert 'event.type is not defined' seems like the 3rd case can't be reached. * Avoided the need for Array.prototype.slice.call( arguments, 1 ) and args.unshift(). * Replaced args[0] for event when looping through the handlers, those were 2 array lookups per handler. * Same for the namespace (parts). * Simplified the val/ret part, ret is not needed and the code is now shorter. * The forced call to preventDefault and stopPropagation is only checked and done once, after the loop. to Hi, here are a few modifications, that I think they will improve perfomance, and some make the code shorter, I'll explain them in case the reason for any change is not clear. jQuery.event.fix * The same event object, can't be fixed more than once (it happens when triggering. jQuery.event.handle * Removed the {} fallback when calling jQuery.event.fix, event.type is used in the next line, so IF you were to fall on the last case, the next line would alert 'event.type is not defined' seems like the 3rd case can't be reached. * Avoided the need for Array.prototype.slice.call( arguments, 1 ) and args.unshift(). * Replaced args[0] for event when looping through the handlers, those were 2 array lookups per handler. * Same for the namespace (parts). * Simplified the val/ret part, ret is not needed and the code is now shorter. * The forced call to preventDefault and stopPropagation is only checked and done once, after the loop.

Changed 5 months ago by brandon

  • status changed from new to closed
  • resolution set to fixed

Mostly applied in Rev [5276].

Just a couple of things different:

  • The val returned from a handler can be a non-boolean value.
  • An event must always be "fixed" in IE due to its global event object... at least that seems to be the case with my testing so far. I'm holding off on optimizing the event.fix method for lots more testing.

BTW ... I was seeing over 200% increase in performance of jQuery.event.trigger in IE. http://brandonaaron.net/jquery/tickets/2655/baseline-performance-test.html http://brandonaaron.net/jquery/tickets/2655/patched-performance-test.html

Changed 5 months ago by flesler

The val returned from a handler can be a non-boolean value

It says:

val = handler.apply( this, arguments ) '''!== false '''&& val;

That means that, unless it is explicitely false, it remains true. The val is not returned so it doesn't matter what it is, as long as it's not false... I might not be getting it, can you clarify me please ?

An event must always be "fixed" in IE

You can set event._fixed_ = null in IE, along with the event.target and the others.

BTW ... I was seeing over 200% increase in performance of jQuery.event.trigger in IE.

You mean with a change(s) I proposed ?

Note: See TracTickets for help on using tickets.