Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active July 9, 2020 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/e0587eaf24f549f4f8255c475b62f304 to your computer and use it in GitHub Desktop.
Save nolanlawson/e0587eaf24f549f4f8255c475b62f304 to your computer and use it in GitHub Desktop.
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