Skip to content

Commit 407aa1f

Browse files
Avoid a double UID lookup when we stop observing all an element's events. (Victor Homyakov, Andrew Dupont)
1 parent 7b09d80 commit 407aa1f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/prototype/dom/event.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -534,15 +534,19 @@
534534

535535
Event._isCustomEvent = isCustomEvent;
536536

537-
function getRegistryForElement(element) {
537+
// These two functions take an optional UID as a second argument so that we
538+
// can skip lookup if we've already got the element's UID.
539+
function getRegistryForElement(element, uid) {
538540
var CACHE = GLOBAL.Event.cache;
539-
var uid = getUniqueElementID(element);
541+
if (Object.isUndefined(uid))
542+
uid = getUniqueElementID(element);
540543
if (!CACHE[uid]) CACHE[uid] = { element: element };
541544
return CACHE[uid];
542545
}
543546

544-
function destroyRegistryForElement(element) {
545-
var uid = getUniqueElementID(element);
547+
function destroyRegistryForElement(element, uid) {
548+
if (Object.isUndefined(uid))
549+
uid = getUniqueElementID(element);
546550
delete GLOBAL.Event.cache[uid];
547551
}
548552

@@ -897,11 +901,17 @@
897901

898902
// Stop observing _all_ listeners on an element.
899903
function stopObservingElement(element) {
900-
var registry = getRegistryForElement(element);
901-
destroyRegistryForElement(element);
904+
var uid = getUniqueElementID(element),
905+
registry = getRegistryForElement(element, uid);
906+
907+
destroyRegistryForElement(element, uid);
902908

903909
var entries, i;
904910
for (var eventName in registry) {
911+
// Explicitly skip elements so we don't accidentally find one with a
912+
// `length` property.
913+
if (eventName === 'element') continue;
914+
905915
entries = registry[eventName];
906916
i = entries.length;
907917
while (i--)

0 commit comments

Comments
 (0)