Brian Mearns
1 min readJul 22, 2019

--

I think this statement might be conflating declaring a variable and instantiating a value. While I’m not deeply familiar with the internals of the JavaScript runtime, I would expect that declaring a local variable would only set aside an additional temporary slot on the stack (just enough to hold a reference) which would be freed as soon as the scope is left.

I’m not sure if each case statement would count as a different scope or not, but worst case for memory (where all the case statements are in the same scope), each declared variable would just be one additional slot pushed temporarily to the stack. Whether each case statement gets its own const, or you have a single shared variable for them to share, only one case statement will actually get executed, so only that one object instantiation would actually take place; the impact on heap memory should be the same either way.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Brian Mearns
Brian Mearns

Written by Brian Mearns

Software Engineer since 2007 ・ Parent ・ Mediocre Runner ・ Flower and Tree Enthusiast ・ Crappy Wood Worker ・ he/him or they/them

Responses (1)