From 24ca582bc5f133bf43004eb87f446fc558fc8c64 Mon Sep 17 00:00:00 2001 From: Andrew Kushnir Date: Thu, 18 Jul 2019 15:05:00 -0700 Subject: [PATCH] perf(compiler): avoid copying from prototype while cloning an object (#31638) This commit updates the `_clone` function of the `_ApplySourceSpanTransformer` class, where the for-in loop was used, resulting in copying from prototype to own properties, thus consuming more memory. Prior to NodeJS 12 (V8 versions before 7.4) there was an optimization that was improving the situation and since that logic was removed in favor of other optimizations, the situation with memory consumption caused by the for-in loop got worse. This commit adds a check to make sure we copy only own properties over to cloned object. Closes #31627. PR Close #31638 --- packages/compiler/src/output/output_ast.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler/src/output/output_ast.ts b/packages/compiler/src/output/output_ast.ts index 7b456e89d6..82d5addb91 100644 --- a/packages/compiler/src/output/output_ast.ts +++ b/packages/compiler/src/output/output_ast.ts @@ -1464,7 +1464,7 @@ class _ApplySourceSpanTransformer extends AstTransformer { constructor(private sourceSpan: ParseSourceSpan) { super(); } private _clone(obj: any): any { const clone = Object.create(obj.constructor.prototype); - for (let prop in obj) { + for (let prop of Object.keys(obj)) { clone[prop] = obj[prop]; } return clone;