1

I am working with rather long strings, and I am worried that when I do

a = "mylongstring"
b = a

that would store the string in memory twice. Since JS strings are immutable, I have no way to verify if it is passed by value (copied) or by reference, and reading about it tells me that "we can assume they are passed by reference, but in actuality it does not matter". Well, it it takes twice the space, it does actually matter, so I'd prefer not assuming. Does it?

N.B. For the story, it is because I have this long string in my React-redux store, and every component listening to changes assigns this string in its own state.

JulienD
  • 7,102
  • 9
  • 50
  • 84
  • 3
    It would depend on the engine's implementation, but we can safely assume that no engine is that stupid to make copies of immutable elements. That's the whole point of them being immutable. – JJJ Sep 02 '16 at 08:53
  • But if you create var a = new String('aaa'); b = a; , it is passed by reference for sure. –  Sep 02 '16 at 08:59

0 Answers0