Skip to content

Instantly share code, notes, and snippets.

@mheiber
Last active October 5, 2018 13:34
Show Gist options
  • Save mheiber/b491164b4cb73fb73b3b3ab3bc7e171f to your computer and use it in GitHub Desktop.
Save mheiber/b491164b4cb73fb73b3b3ab3bc7e171f to your computer and use it in GitHub Desktop.
closures
const main = () => {
const x = 2
const y = 3
;[1, 2, 3].forEach(v => {
console.log(v, y)
})
console.log(x, y)
}
main()

I think V8 optimizes away x in the forEach callback since it is not used there. See the Closures listed in the debugger.

screen shot 2018-10-04 at 10 11 45 pm

And if you don't access y, it goes away, too.

I would be surprised if this mattered either way, since the function's stack frame is short-lived.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment