Skip to content

Instantly share code, notes, and snippets.

@nolanlawson

nolanlawson/index.html

Last active Jul 9, 2020
Embed
What would you like to do?
Demo heap snapshot retainment
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo heap snapshot retainment</title>
</head>
<body>
<h1>Demo heap snapshot retainment</h1>
<button>Add a memory leak</button>
<script>
(() => {
const weakMap = new WeakMap()
class BarObject {
constructor() {
this.circularReference = () => console.log(this)
}
}
class FooObject {
constructor() {
const barObject = new BarObject()
this.barObject = barObject
weakMap.set(barObject, this)
}
}
document.querySelector('button').addEventListener('click', () => {
const objectReferencedByClosure = new FooObject()
window.addEventListener('resize', () => {
console.log(objectReferencedByClosure)
})
})
})()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment