From 2f11ba7c98d4d2d2157b7ae554f57f1798b04306 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Wed, 6 Apr 2011 23:41:47 -0400 Subject: [PATCH 1/3] Allow specific custom events to exit trigger early if there are no handlers bound for that type. --- src/event.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/event.js b/src/event.js index 9a63b5f93..d39bf7ac0 100644 --- a/src/event.js +++ b/src/event.js @@ -276,6 +276,14 @@ jQuery.event = { } } }, + + // Events that are safe to short-circuit if no handlers are attached. + // Native DOM events should not be added, they may have inline handlers. + customEvent: { + "getData": true, + "setData": true, + "changeData": true + }, trigger: function( event, data, elem ) { // Event object or event type @@ -293,7 +301,7 @@ jQuery.event = { jQuery.Event(type); if ( type.indexOf("!") >= 0 ) { - // Exclusive events trigger only for the bare event type (no namespaces) + // Exclusive events trigger only for the bare event type (no namespaces) event.type = type = type.slice(0, -1); event.exclusive = true; } @@ -305,7 +313,12 @@ jQuery.event = { } event.namespace = namespaces.join("."); event.namespace_re = new RegExp("(^|\\.)" + namespaces.join("\\.(?:.*\\.)?") + "(\\.|$)"); - + + if ( jQuery.event.customEvent[ type ] && !jQuery.event.global[ type ] ) { + // No jQuery handlers for this event type, and it can't have inline handlers + return; + } + // Handle a global trigger if ( !elem ) { // Don't bubble custom events when global (to avoid too much overhead) From 530c915553dd21f7f933d6d1ca8102ae714f5aa2 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Mon, 11 Apr 2011 11:15:00 -0400 Subject: [PATCH 2/3] Move initialization of event until we determine if anyone wants it. --- src/event.js | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/event.js b/src/event.js index 3d19e50c2..95f305bab 100644 --- a/src/event.js +++ b/src/event.js @@ -289,8 +289,27 @@ jQuery.event = { trigger: function( event, data, elem ) { // Event object or event type var type = event.type || event, - namespaces = []; + namespaces = [], + exclusive; + if ( type.indexOf("!") >= 0 ) { + // Exclusive events trigger only for the exact event (no namespaces) + type = type.slice(0, -1); + exclusive = true; + } + if ( type.indexOf(".") >= 0 ) { + // Namespaced trigger; create a regexp to match event type in handle() + namespaces = type.split("."); + type = namespaces.shift(); + namespaces.sort(); + } + + if ( jQuery.event.customEvent[ type ] && !jQuery.event.global[ type ] ) { + // No jQuery handlers for this event type, and it can't have inline handlers + return; + } + + // Caller can pass in an Event, Object, or just an event type string event = typeof event === "object" ? // jQuery.Event object event[ jQuery.expando ] ? event : @@ -298,25 +317,9 @@ jQuery.event = { jQuery.extend( jQuery.Event(type), event ) : // Just the event type (string) jQuery.Event(type); - - if ( type.indexOf("!") >= 0 ) { - // Exclusive events trigger only for the bare event type (no namespaces) - event.type = type = type.slice(0, -1); - event.exclusive = true; - } - if ( type.indexOf(".") >= 0 ) { - // Namespaced trigger; create a regexp to match event type in handle() - namespaces = type.split("."); - event.type = type = namespaces.shift(); - namespaces.sort(); - } event.namespace = namespaces.join("."); event.namespace_re = new RegExp("(^|\\.)" + namespaces.join("\\.(?:.*\\.)?") + "(\\.|$)"); - - if ( jQuery.event.customEvent[ type ] && !jQuery.event.global[ type ] ) { - // No jQuery handlers for this event type, and it can't have inline handlers - return; - } + event.exclusive = exclusive; // Handle a global trigger if ( !elem ) { From a5071d49ba29ba115954845db9363aa6459f1a84 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Mon, 11 Apr 2011 13:19:00 -0400 Subject: [PATCH 3/3] Shave some time off array setup in trigger/handle --- src/event.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/event.js b/src/event.js index e6e7788da..097bc6501 100644 --- a/src/event.js +++ b/src/event.js @@ -354,7 +354,7 @@ jQuery.event = { event.target = elem; // Clone any incoming data and prepend the event, creating the handler arg list - data = jQuery.makeArray( data ); + data = data? jQuery.makeArray( data ) : []; data.unshift( event ); var cur = elem, @@ -419,7 +419,7 @@ jQuery.event = { // Snapshot the handlers list since a called handler may add/remove events. var handlers = ((jQuery._data( this, "events" ) || {})[ event.type ] || []).slice(0), run_all = !event.exclusive && !event.namespace, - args = jQuery.makeArray( arguments ); + args = Array.prototype.slice.call( arguments, 0 ); // Use the fix-ed Event rather than the (read-only) native event args[0] = event;