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

Commit e822e90

Browse files
committed
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
1 parent 301463a commit e822e90

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/ng/rootScope.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -562,16 +562,18 @@ function $RootScopeProvider(){
562562
for (key in newValue) {
563563
if (newValue.hasOwnProperty(key)) {
564564
newLength++;
565-
if (oldValue.hasOwnProperty(key)) {
566-
bothNaN = (oldValue[key] !== oldValue[key]) &&
567-
(newValue[key] !== newValue[key]);
568-
if (!bothNaN && (oldValue[key] !== newValue[key])) {
565+
newItem = newValue[key];
566+
oldItem = oldValue[key];
567+
568+
if (key in oldValue) {
569+
bothNaN = (oldItem !== oldItem) && (newItem !== newItem);
570+
if (!bothNaN && (oldItem !== newItem)) {
569571
changeDetected++;
570-
oldValue[key] = newValue[key];
572+
oldValue[key] = newItem;
571573
}
572574
} else {
573575
oldLength++;
574-
oldValue[key] = newValue[key];
576+
oldValue[key] = newItem;
575577
changeDetected++;
576578
}
577579
}
@@ -580,7 +582,7 @@ function $RootScopeProvider(){
580582
// we used to have more keys, need to find them and destroy them.
581583
changeDetected++;
582584
for(key in oldValue) {
583-
if (oldValue.hasOwnProperty(key) && !newValue.hasOwnProperty(key)) {
585+
if (!newValue.hasOwnProperty(key)) {
584586
oldLength--;
585587
delete oldValue[key];
586588
}

0 commit comments

Comments
 (0)