Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do common JavaScript implementations use string interning?

Do common JavaScript engines, such as V8 and WebKit's JavaScriptCore, use string interning for JavaScript strings? Or do they actually keep multiple instances of identical strings in memory?

like image 627
kpozin Avatar asked Mar 11 '11 18:03

kpozin


People also ask

Does JavaScript have string pool?

Well, no, they each get a string primitive. Granted it a subtle distinction, but JavaScript has both: var sp = "primitive"; var so = new String("object");

What does string intern () method do?

The method intern() creates an exact copy of a String object in the heap memory and stores it in the String constant pool. Note that, if another String with the same contents exists in the String constant pool, then a new object won't be created and the new reference will point to the other String.

Are Python strings interned?

Python automatically interns some strings at the moment of their creation. Whether or not a string is interned depends on several factors: All empty strings and strings of length 1 are interned. Up until version 3.7, Python used peephole optimization, and all strings longer than 20 characters were not interned.

Why intern method is used in Java?

The intern() method is used to store strings in the String constant pool and is majorly used if we want the strings present in the heap to be stored in the String constant pool. A string pool is similar to the way objects are allocated. It is empty by default and it is a part of the Java String class.


1 Answers

Yes. In general any literal string, identifier, or other constant string in JS source is interned. However implementation details (exactly what is interned for instance) varies, as well as when the interning occurs.

Note that a string value is not the same as a String Object though, String Objects are not interned because that would be fundamentally incorrect behaviour.

like image 128
olliej Avatar answered Sep 22 '22 05:09

olliej