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

Commit fcd2a81

Browse files
kseamonIgorMinar
authored andcommitted
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
1 parent 62dbe85 commit fcd2a81

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/ngResource/resource.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ function lookupDottedPath(obj, path) {
2424
return obj;
2525
}
2626

27+
/**
28+
* Create a shallow copy of an object and clear other fields from the destination
29+
*/
30+
function shallowClearAndCopy(src, dst) {
31+
dst = dst || {};
32+
33+
angular.forEach(dst, function(value, key){
34+
delete dst[key];
35+
});
36+
37+
for (var key in src) {
38+
if (src.hasOwnProperty(key) && key.substr(0, 2) !== '$$') {
39+
dst[key] = src[key];
40+
}
41+
}
42+
43+
return dst;
44+
}
45+
2746
/**
2847
* @ngdoc overview
2948
* @name ngResource
@@ -393,7 +412,7 @@ angular.module('ngResource', ['ng']).
393412
}
394413

395414
function Resource(value){
396-
copy(value || {}, this);
415+
shallowClearAndCopy(value || {}, this);
397416
}
398417

399418
forEach(actions, function(action, name) {
@@ -465,7 +484,7 @@ angular.module('ngResource', ['ng']).
465484
if (data) {
466485
// Need to convert action.isArray to boolean in case it is undefined
467486
// jshint -W018
468-
if ( angular.isArray(data) !== (!!action.isArray) ) {
487+
if (angular.isArray(data) !== (!!action.isArray)) {
469488
throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' +
470489
'response to contain an {0} but got an {1}',
471490
action.isArray?'array':'object', angular.isArray(data)?'array':'object');
@@ -477,7 +496,7 @@ angular.module('ngResource', ['ng']).
477496
value.push(new Resource(item));
478497
});
479498
} else {
480-
copy(data, value);
499+
shallowClearAndCopy(data, value);
481500
value.$promise = promise;
482501
}
483502
}

0 commit comments

Comments
 (0)