mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-08 14:54:44 +00:00
perf($resource): use shallow copy instead of angular.copy
Replace calls to angular.copy with calls to a new function, shallowClearAndCopy. Add calls to copy for cache access in $http in order to prevent modification of cached data. Results in a measurable improvement to the startup time of complex apps within Google. Closes #5300
This commit is contained in:
parent
62dbe85798
commit
fcd2a8131a
1 changed files with 22 additions and 3 deletions
|
|
@ -24,6 +24,25 @@ function lookupDottedPath(obj, path) {
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a shallow copy of an object and clear other fields from the destination
|
||||||
|
*/
|
||||||
|
function shallowClearAndCopy(src, dst) {
|
||||||
|
dst = dst || {};
|
||||||
|
|
||||||
|
angular.forEach(dst, function(value, key){
|
||||||
|
delete dst[key];
|
||||||
|
});
|
||||||
|
|
||||||
|
for (var key in src) {
|
||||||
|
if (src.hasOwnProperty(key) && key.substr(0, 2) !== '$$') {
|
||||||
|
dst[key] = src[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ngdoc overview
|
* @ngdoc overview
|
||||||
* @name ngResource
|
* @name ngResource
|
||||||
|
|
@ -393,7 +412,7 @@ angular.module('ngResource', ['ng']).
|
||||||
}
|
}
|
||||||
|
|
||||||
function Resource(value){
|
function Resource(value){
|
||||||
copy(value || {}, this);
|
shallowClearAndCopy(value || {}, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
forEach(actions, function(action, name) {
|
forEach(actions, function(action, name) {
|
||||||
|
|
@ -465,7 +484,7 @@ angular.module('ngResource', ['ng']).
|
||||||
if (data) {
|
if (data) {
|
||||||
// Need to convert action.isArray to boolean in case it is undefined
|
// Need to convert action.isArray to boolean in case it is undefined
|
||||||
// jshint -W018
|
// jshint -W018
|
||||||
if ( angular.isArray(data) !== (!!action.isArray) ) {
|
if (angular.isArray(data) !== (!!action.isArray)) {
|
||||||
throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' +
|
throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' +
|
||||||
'response to contain an {0} but got an {1}',
|
'response to contain an {0} but got an {1}',
|
||||||
action.isArray?'array':'object', angular.isArray(data)?'array':'object');
|
action.isArray?'array':'object', angular.isArray(data)?'array':'object');
|
||||||
|
|
@ -477,7 +496,7 @@ angular.module('ngResource', ['ng']).
|
||||||
value.push(new Resource(item));
|
value.push(new Resource(item));
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
copy(data, value);
|
shallowClearAndCopy(data, value);
|
||||||
value.$promise = promise;
|
value.$promise = promise;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue