From e822e9061c2a605649d91abbd641f757e2829275 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Mon, 11 Aug 2014 19:08:55 -0700 Subject: [PATCH] perf(Scope): optimize $watchCollection when used for watching objects Since we control the oldValue, we don't need to worry about proto-inhereted properties which means we can use 'for in' and skip hasOwnProperty checks. http://jsperf.com/for-in-vs-object-keys2 --- src/ng/rootScope.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/ng/rootScope.js b/src/ng/rootScope.js index 36082479..30f2fa0e 100644 --- a/src/ng/rootScope.js +++ b/src/ng/rootScope.js @@ -562,16 +562,18 @@ function $RootScopeProvider(){ for (key in newValue) { if (newValue.hasOwnProperty(key)) { newLength++; - if (oldValue.hasOwnProperty(key)) { - bothNaN = (oldValue[key] !== oldValue[key]) && - (newValue[key] !== newValue[key]); - if (!bothNaN && (oldValue[key] !== newValue[key])) { + newItem = newValue[key]; + oldItem = oldValue[key]; + + if (key in oldValue) { + bothNaN = (oldItem !== oldItem) && (newItem !== newItem); + if (!bothNaN && (oldItem !== newItem)) { changeDetected++; - oldValue[key] = newValue[key]; + oldValue[key] = newItem; } } else { oldLength++; - oldValue[key] = newValue[key]; + oldValue[key] = newItem; changeDetected++; } } @@ -580,7 +582,7 @@ function $RootScopeProvider(){ // we used to have more keys, need to find them and destroy them. changeDetected++; for(key in oldValue) { - if (oldValue.hasOwnProperty(key) && !newValue.hasOwnProperty(key)) { + if (!newValue.hasOwnProperty(key)) { oldLength--; delete oldValue[key]; }