-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Memory leak when deep cloning in
babel-core
(#14583)
* fix * review * review * fix and rebase * improve typing * review * fix test
- Loading branch information
1 parent
f0ec180
commit e482c76
Showing
4 changed files
with
70 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
packages/babel-core/src/transformation/util/clone-deep-browser.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,32 @@ | ||
import v8 from "v8"; | ||
import cloneDeep from "./clone-deep-browser"; | ||
|
||
export default function (value) { | ||
if (v8.deserialize && v8.serialize) { | ||
return v8.deserialize(v8.serialize(value)); | ||
//https://github.com/babel/babel/pull/14583#discussion_r882828856 | ||
function deepClone(value: any, cache: Map<any, any>): any { | ||
if (value !== null) { | ||
if (cache.has(value)) return cache.get(value); | ||
let cloned: any; | ||
if (Array.isArray(value)) { | ||
cloned = new Array(value.length); | ||
for (let i = 0; i < value.length; i++) { | ||
cloned[i] = | ||
typeof value[i] !== "object" ? value[i] : deepClone(value[i], cache); | ||
} | ||
} else { | ||
cloned = {}; | ||
const keys = Object.keys(value); | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
cloned[key] = | ||
typeof value[key] !== "object" | ||
? value[key] | ||
: deepClone(value[key], cache); | ||
} | ||
} | ||
cache.set(value, cloned); | ||
return cloned; | ||
} | ||
return cloneDeep(value); | ||
return value; | ||
} | ||
|
||
export default function <T>(value: T): T { | ||
if (typeof value !== "object") return value; | ||
return deepClone(value, new Map()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters