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

Commit 13d1a36

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 19871d2 commit 13d1a36

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/ng/rootScope.js

+12-12
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
};
@@ -871,11 +869,10 @@ function $RootScopeProvider(){
871869
// - https://github.com/angular/angular.js/issues/1313#issuecomment-10378451
872870

873871
this.$parent = this.$$nextSibling = this.$$prevSibling = this.$$childHead =
874-
this.$$childTail = this.$root = null;
872+
this.$$childTail = this.$root = this.$$watchers = this.$$listeners = null;
875873

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

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

10871084
var current = this;
10881085
do {
1086+
if (!current.$$listenerCount) {
1087+
current.$$listenerCount = {};
1088+
}
10891089
if (!current.$$listenerCount[name]) {
10901090
current.$$listenerCount[name] = 0;
10911091
}
@@ -1140,7 +1140,7 @@ function $RootScopeProvider(){
11401140
i, length;
11411141

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

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

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

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

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

0 commit comments

Comments
 (0)