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
This commit is contained in:
Igor Minar
2014-08-11 19:08:55 -07:00
parent 301463a2e2
commit e822e9061c

View File

@@ -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];
}