Last active
November 11, 2017 16:54
-
-
Save joegaudet/fc26b73614ccc2d59839f7786bdb109d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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