Skip to content
Permalink
Browse files

Since we fixed .is(POS) let's use it and save bytes.

Also, creates a single jQuery object and reuses it in the delegation-test loop to make it more performancy.
  • Loading branch information...
dmethvin committed Nov 7, 2011
1 parent f5250e1 commit a7e911b7fe84a35234d4be18102e168c6644e40d
Showing with 12 additions and 16 deletions.
  1. +12 −16 src/event.js
@@ -99,17 +99,10 @@ jQuery.event = {
handler: handler,
guid: handler.guid,
selector: selector,
quick: quickParse( selector ),
namespace: namespaces.join(".")
}, handleObjIn );

// Delegated event; pre-analyze selector so it's processed quickly on event dispatch
if ( selector ) {
handleObj.quick = quickParse( selector );
if ( !handleObj.quick && jQuery.expr.match.POS.test( selector ) ) {
handleObj.isPositional = true;
}
}

// Init the event handler queue if we're the first
handlers = events[ type ];
if ( !handlers ) {
@@ -406,7 +399,7 @@ jQuery.event = {
run_all = !event.exclusive && !event.namespace,
specialHandle = ( jQuery.event.special[ event.type ] || {} ).handle,
handlerQueue = [],
i, j, cur, ret, selMatch, matched, matches, handleObj, sel, hit, related;
i, j, cur, jqcur, ret, selMatch, matched, matches, handleObj, sel, related;

// Use the fix-ed jQuery.Event rather than the (read-only) native event
args[0] = event;
@@ -416,21 +409,24 @@ jQuery.event = {
// Avoid disabled elements in IE (#6911) and non-left-click bubbling in Firefox (#3861)
if ( delegateCount && !event.target.disabled && !(event.button && event.type === "click") ) {

// Pregenerate a single jQuery object for reuse with .is()
jqcur = jQuery(this);
jqcur.context = this.ownerDocument || this;

for ( cur = event.target; cur != this; cur = cur.parentNode || this ) {
selMatch = {};
matches = [];
jqcur[0] = cur;
for ( i = 0; i < delegateCount; i++ ) {
handleObj = handlers[ i ];
sel = handleObj.selector;
hit = selMatch[ sel ];

if ( handleObj.isPositional ) {
// Since .is() does not work for positionals; see http://jsfiddle.net/eJ4yd/3/
hit = ( hit || (selMatch[ sel ] = jQuery( sel )) ).index( cur ) >= 0;
} else if ( hit === undefined ) {
hit = selMatch[ sel ] = ( handleObj.quick ? quickIs( cur, handleObj.quick ) : jQuery( cur ).is( sel ) );
if ( selMatch[ sel ] === undefined ) {
selMatch[ sel ] = (
handleObj.quick ? quickIs( cur, handleObj.quick ) : jqcur.is( sel )
);
}
if ( hit ) {
if ( selMatch[ sel ] ) {
matches.push( handleObj );
}
}

1 comment on commit a7e911b

@mathiasbynens

This comment has been minimized.

Copy link
Contributor

mathiasbynens commented on a7e911b Nov 7, 2011

Moar performancy! ♥

Please sign in to comment.
You can’t perform that action at this time.