0

Does the behaviour of javascript is the same as in Java, which works with a String constant pool?

In other terms, when I use:

switch(foo){
  case 'baby': break
  case 'bacoco': break
}

… does a new instance created for 'baby' (and maybe for 'bacoco') each time the program runs the switch cases?

And also a new one when it runs:


if('baby' === otherfoo){}

Thanks for answers.

  • 3
    strings are not objects, and any kind of optimisation behaviour for this sort of thing would be an implementation detail, not a language feature. – Quentin May 14 '19 at 12:42
  • 1
    Possible duplicate of [Is there a String pool concept in JavaScript? Can we get values/keys to refer to just one String object?](https://stackoverflow.com/questions/39449174/is-there-a-string-pool-concept-in-javascript-can-we-get-values-keys-to-refer-to) – spender May 14 '19 at 12:43
  • Also: https://stackoverflow.com/questions/5276915/do-common-javascript-implementations-use-string-interning – spender May 14 '19 at 12:44
  • You're both right… But I can't understand theses questions… (I didn't know the String pool concept in Java, I found it during my current search). So what should I do? Delete my question? Thanks for your advices. I could also extend the question. With the use of a constant — say: 'const STRbaby = "baby"' — and use it. – Philippe Perret May 14 '19 at 13:06

0 Answers0