Skip to content

Instantly share code, notes, and snippets.

@joegaudet
Last active November 11, 2017 16:54
Show Gist options
  • Save joegaudet/fc26b73614ccc2d59839f7786bdb109d to your computer and use it in GitHub Desktop.
Save joegaudet/fc26b73614ccc2d59839f7786bdb109d to your computer and use it in GitHub Desktop.
function aFunction() {
// primitives are stored directly on the stack
const foo = 1;
// obects which do not escape the scope of a function will
// be allocated on the stack and will be freed automatically
// on return
const bar = {
stack: 123
};
// object which escape the scope of the function will be allocated
// on the heap and garbage collected once there are no more references
// pointing to them
const baz = {
heap: 123
}
return baz;
}
// since the return is assigned to a value it will remain on the heap
let val = aFunction();
// now that we have set val to null the gc will have an opportunity to
// free the object
val = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment