angular.copy Very poor performance (large objects) #11099
Description
Our application loads resources from API and renders them in a table. API request takes ~200ms, rendering table ~140ms but when you actually looking at the page it takes about 6s to display it.
I have drilled through the code and realized this was caused by using angular.copy . I need to keep track of the old data so I copied the whole object to backup object via angular.copy() function. The copying itself took over 2s (~2500ms).
I have searched for better ways, to copy objects and stumbled upon this code:
cloneObject: function(o){
const gdcc = "__getDeepCircularCopy__";
if (o !== Object(o)) {
return o; // primitive value
}
var set = gdcc in o,
cache = o[gdcc],
result;
if (set && typeof cache == "function") {
return cache();
}
// else
o[gdcc] = function() { return result; }; // overwrite
if (o instanceof Array) {
result = [];
for (var i=0; i<o.length; i++) {
result[i] = this.cloneObject(o[i]);
}
} else {
result = {};
for (var prop in o)
if (prop != gdcc)
result[prop] = this.cloneObject(o[prop]);
else if (set)
result[prop] = this.cloneObject(cache);
}
if (set) {
o[gdcc] = cache; // reset
} else {
delete o[gdcc]; // unset again
}
return result;
},
This is my implementation of this: http://stackoverflow.com/a/10729242/820942
Replacing all angular.copy() in my code with this hugely improved the webapp overall. The same example above takes ~41ms instead of ~2s.
I should mention that the data I am mentioning is pretty large (renders into 10 000 cells table)
After very brief look on the angular.copy code I am aware of that there are some added angular goodies ($$hashkey) but still the difference in performance is just too big.