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
This commit is contained in:
Andrew Kushnir 2019-07-18 15:05:00 -07:00 committed by Kara Erickson
parent b31a292955
commit 24ca582bc5
1 changed files with 1 additions and 1 deletions

View File

@ -1464,7 +1464,7 @@ class _ApplySourceSpanTransformer extends AstTransformer {
constructor(private sourceSpan: ParseSourceSpan) { super(); } constructor(private sourceSpan: ParseSourceSpan) { super(); }
private _clone(obj: any): any { private _clone(obj: any): any {
const clone = Object.create(obj.constructor.prototype); const clone = Object.create(obj.constructor.prototype);
for (let prop in obj) { for (let prop of Object.keys(obj)) {
clone[prop] = obj[prop]; clone[prop] = obj[prop];
} }
return clone; return clone;