Created
April 13, 2022 14:44
-
-
Save subzey/1522e6d926b24a10360ad6ec85ccfdac to your computer and use it in GitHub Desktop.
"<symbol> in Promise" leak
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
<!doctype html> | |
<script> | |
let singletonPromise; | |
function makeSomeRequest() { | |
if (!singletonPromise) { | |
singletonPromise = Promise.resolve('(Response from some request that only should be made once)') | |
} | |
return singletonPromise; | |
} | |
class LeakyClass { | |
constructor() { | |
// Makes the memory leak more apparent | |
this._heavyObject = new Array(8 * 1024 * 1024).fill(0); | |
} | |
async doAsyncStuff() { | |
console.log(await makeSomeRequest()); | |
} | |
} | |
new LeakyClass().doAsyncStuff(); | |
</script> | |
<p>Try refreshing this page with Chrome DevTools open. The <code>LeakyClass</code> instance would not be garbage collected. The heap size would be around 32 MB.</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment