Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 5b426f6

Browse files
committed
perf($rootScope): only creating $$listeners and $$listenerCount when required
- this avoids creating 2 JSON objects per scope until: registering a listener ($$listeners) or a child registering a listener ($$listenerCount)
1 parent d219a57 commit 5b426f6

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/ng/rootScope.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ function $RootScopeProvider(){
132132
this.$$destroyed = false;
133133
this.$$asyncQueue = [];
134134
this.$$postDigestQueue = [];
135-
this.$$listeners = {};
136-
this.$$listenerCount = {};
135+
this.$$listeners = this.$$listenerCount = null;
137136
this.$$isolateBindings = null;
138137
this.$$applyAsyncQueue = [];
139138
}
@@ -203,8 +202,7 @@ function $RootScopeProvider(){
203202
this.$$ChildScope = function ChildScope() {
204203
this.$$watchers = this.$$nextSibling =
205204
this.$$childHead = this.$$childTail = null;
206-
this.$$listeners = {};
207-
this.$$listenerCount = {};
205+
this.$$listeners = this.$$listenerCount = null;
208206
this.$id = nextUid();
209207
this.$$ChildScope = null;
210208
};
@@ -870,11 +868,10 @@ function $RootScopeProvider(){
870868
// - https://github.com/angular/angular.js/issues/1313#issuecomment-10378451
871869

872870
this.$parent = this.$$nextSibling = this.$$prevSibling = this.$$childHead =
873-
this.$$childTail = this.$root = null;
871+
this.$$childTail = this.$root = this.$$watchers = this.$$listeners = null;
874872

875873
// don't reset these to null in case some async task tries to register a listener/watch/task
876-
this.$$listeners = {};
877-
this.$$watchers = this.$$asyncQueue = this.$$postDigestQueue = [];
874+
this.$$asyncQueue = this.$$postDigestQueue = [];
878875

879876
// prevent NPEs since these methods have references to properties we nulled out
880877
this.$destroy = this.$digest = this.$apply = noop;
@@ -1077,14 +1074,17 @@ function $RootScopeProvider(){
10771074
* @returns {function()} Returns a deregistration function for this listener.
10781075
*/
10791076
$on: function(name, listener) {
1080-
var namedListeners = this.$$listeners[name];
1077+
var namedListeners = (this.$$listeners || (this.$$listeners = {}))[name];
10811078
if (!namedListeners) {
10821079
this.$$listeners[name] = namedListeners = [];
10831080
}
10841081
namedListeners.push(listener);
10851082

10861083
var current = this;
10871084
do {
1085+
if (!current.$$listenerCount) {
1086+
current.$$listenerCount = {};
1087+
}
10881088
if (!current.$$listenerCount[name]) {
10891089
current.$$listenerCount[name] = 0;
10901090
}
@@ -1139,7 +1139,7 @@ function $RootScopeProvider(){
11391139
i, length;
11401140

11411141
do {
1142-
namedListeners = scope.$$listeners[name] || empty;
1142+
namedListeners = scope.$$listeners && scope.$$listeners[name] || empty;
11431143
event.currentScope = scope;
11441144
for (i=0, length=namedListeners.length; i<length; i++) {
11451145

@@ -1206,15 +1206,15 @@ function $RootScopeProvider(){
12061206
defaultPrevented: false
12071207
};
12081208

1209-
if (!target.$$listenerCount[name]) return event;
1209+
if (!(target.$$listenerCount && target.$$listenerCount[name])) return event;
12101210

12111211
var listenerArgs = concat([event], arguments, 1),
12121212
listeners, i, length;
12131213

12141214
//down while you can, then up and next sibling or up and next sibling until back at root
12151215
while ((current = next)) {
12161216
event.currentScope = current;
1217-
listeners = current.$$listeners[name] || [];
1217+
listeners = current.$$listeners && current.$$listeners[name] || [];
12181218
for (i=0, length = listeners.length; i<length; i++) {
12191219
// if listeners were deregistered, defragment the array
12201220
if (!listeners[i]) {
@@ -1235,7 +1235,7 @@ function $RootScopeProvider(){
12351235
// yes, this code is a bit crazy, but it works and we have tests to prove it!
12361236
// this piece should be kept in sync with the traversal in $digest
12371237
// (though it differs due to having the extra check for $$listenerCount)
1238-
if (!(next = ((current.$$listenerCount[name] && current.$$childHead) ||
1238+
if (!(next = ((current.$$listenerCount && current.$$listenerCount[name] && current.$$childHead) ||
12391239
(current !== target && current.$$nextSibling)))) {
12401240
while(current !== target && !(next = current.$$nextSibling)) {
12411241
current = current.$parent;

0 commit comments

Comments
 (0)