Skip to content

Commit 5606fe8

Browse files
committed
refactor(copy): avoid creating source/dest Map for simple copies
1 parent 6a102ef commit 5606fe8

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/Angular.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ var ES6Map = isFunction(window.Map) && toString.call(window.Map.prototype) === '
849849
</example>
850850
*/
851851
function copy(source, destination) {
852-
var stack = new ES6Map();
852+
var stack;
853853

854854
if (destination) {
855855
if (isTypedArray(destination) || isArrayBuffer(destination)) {
@@ -870,13 +870,14 @@ function copy(source, destination) {
870870
});
871871
}
872872

873-
stack.set(source, destination);
874873
return copyRecurse(source, destination);
875874
}
876875

877876
return copyElement(source);
878877

879878
function copyRecurse(source, destination) {
879+
(stack || (stack = new ES6Map())).set(source, destination);
880+
880881
var h = destination.$$hashKey;
881882
var key;
882883
if (isArray(source)) {
@@ -914,7 +915,7 @@ function copy(source, destination) {
914915
}
915916

916917
// Already copied values
917-
var existingCopy = stack.get(source);
918+
var existingCopy = stack && stack.get(source);
918919
if (existingCopy) {
919920
return existingCopy;
920921
}
@@ -924,19 +925,15 @@ function copy(source, destination) {
924925
'Can\'t copy! Making copies of Window or Scope instances is not supported.');
925926
}
926927

927-
var needsRecurse = false;
928928
var destination = copyType(source);
929929

930930
if (destination === undefined) {
931-
destination = isArray(source) ? [] : Object.create(getPrototypeOf(source));
932-
needsRecurse = true;
931+
destination = copyRecurse(source, isArray(source) ? [] : Object.create(getPrototypeOf(source)));
932+
} else if (stack) {
933+
stack.set(source, destination);
933934
}
934935

935-
stack.set(source, destination);
936-
937-
return needsRecurse
938-
? copyRecurse(source, destination)
939-
: destination;
936+
return destination;
940937
}
941938

942939
function copyType(source) {

0 commit comments

Comments
 (0)