From 8377e81827a840b9eb64f119de4bcbaba0ceb3be Mon Sep 17 00:00:00 2001 From: James deBoer Date: Sun, 20 Apr 2014 08:24:01 -0700 Subject: [PATCH] perf(scope): 10x. Share the child scope class. This change causes Scope.$destory to run 10x faster. I suspect Scope.$new is significantly faster as well, but I didn't measure it. --- src/ng/rootScope.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 274b36e8..0936ef6a 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -181,18 +181,23 @@ function $RootScopeProvider(){ child.$$asyncQueue = this.$$asyncQueue; child.$$postDigestQueue = this.$$postDigestQueue; } else { - ChildScope = function() {}; // should be anonymous; This is so that when the minifier munges - // the name it does not become random set of chars. This will then show up as class - // name in the web inspector. - ChildScope.prototype = this; - child = new ChildScope(); - child.$id = nextUid(); + // Only create a child scope class if somebody asks for one, + // but cache it to allow the VM to optimize lookups. + if (!this.$$childScopeClass) { + this.$$childScopeClass = function() { + this.$$watchers = this.$$nextSibling = + this.$$childHead = this.$$childTail = null; + this.$$listeners = {}; + this.$$listenerCount = {}; + this.$id = nextUid(); + this.$$childScopeClass = null; + }; + this.$$childScopeClass.prototype = this; + } + child = new this.$$childScopeClass(); } child['this'] = child; - child.$$listeners = {}; - child.$$listenerCount = {}; child.$parent = this; - child.$$watchers = child.$$nextSibling = child.$$childHead = child.$$childTail = null; child.$$prevSibling = this.$$childTail; if (this.$$childHead) { this.$$childTail.$$nextSibling = child;