Faster Listener Deletion

Whenever a component is unmounted, we delete all listeners that might have been attached. This sucks because most applications, Facebook included, do not use every listener. There's a lot of wasted computation, especially if many components are mounted and unmounted.

This changes `deleteAllListeners` to more delete listeners more efficiently.
This commit is contained in:
Tim Yung
2013-07-03 11:38:34 -07:00
committed by Paul O’Shannessy
parent c692d9e844
commit 510146eb6d
2 changed files with 15 additions and 14 deletions
+14 -3
View File
@@ -35,7 +35,7 @@ var CallbackRegistry = {
/**
* Stores `listener` at `listenerBank[registrationName][id]`. Is idempotent.
*
* @param {string} id ID of the DOM node.
* @param {string} id ID of the DOM element.
* @param {string} registrationName Name of listener (e.g. `onClick`).
* @param {?function} listener The callback to store.
*/
@@ -46,7 +46,7 @@ var CallbackRegistry = {
},
/**
* @param {string} id ID of the DOM node.
* @param {string} id ID of the DOM element.
* @param {string} registrationName Name of listener (e.g. `onClick`).
* @return {?function} The stored callback.
*/
@@ -58,7 +58,7 @@ var CallbackRegistry = {
/**
* Deletes a listener from the registration bank.
*
* @param {string} id ID of the DOM node.
* @param {string} id ID of the DOM element.
* @param {string} registrationName Name of listener (e.g. `onClick`).
*/
deleteListener: function(id, registrationName) {
@@ -68,6 +68,17 @@ var CallbackRegistry = {
}
},
/**
* Deletes all listeners for the DOM element with the supplied ID.
*
* @param {string} id ID of the DOM element.
*/
deleteAllListeners: function(id) {
for (var registrationName in listenerBank) {
delete listenerBank[registrationName][id];
}
},
/**
* This is needed for tests only. Do not use!
*/
+1 -11
View File
@@ -112,17 +112,7 @@ var EventPluginHub = {
deleteListener: CallbackRegistry.deleteListener,
/**
* Deletes all listeners for the DOM element with the supplied ID.
*
* @param {string} domID ID of a DOM element.
*/
deleteAllListeners: function(domID) {
var registrationNamesKeys = EventPluginRegistry.registrationNamesKeys;
for (var ii = 0; ii < registrationNamesKeys.length; ii++) {
CallbackRegistry.deleteListener(domID, registrationNamesKeys[ii]);
}
},
deleteAllListeners: CallbackRegistry.deleteAllListeners,
/**
* Allows registered plugins an opportunity to extract events from top-level