I'm working on a project that has both Angular and Underscore as a dependency.
When I need to create a copy of an object, depending on my mood at the time, I might use angular.copy()
or _.clone()
It occurs to me that one of these methods is probably more fast/reliable/robust than the other.
Are there any known issue with either of these functions that make it better or worse to use than the other, assuming both libraries are already included?
We had some bug reports confirming that using angular.copy does create empty objects on some Windows mobile phones. So if you need to support any version of IE on mobile, don't use angular.copy! Allegedly this bug has been fixed by microsoft, but nevertheless we had to deal with it...
Actually, you can as well use
Object.assign()
...Docs: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
Further examples: https://googlechrome.github.io/samples/object-assign-es6/
I know it says no IE, but i tried it on my IE11 and it works...
Regarding your question: angular.copy and _.clone are different. It's not a question of which is better, it is about what you need as @Kevin B stated in the comments.
angular.extend() on the other hand, is a shallow copy akin to _.clone
Angular.copy vs Angular.extend
Performance wise, i'm not sure which is better, but for opinions sake, i'm opposed to including libraries into the global scope (underscore) with any angular app, as usually these things are written as angular modules. angular.copy/angular.extend would win in this case.
Shallow/Deep Copy:
Source