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?
3 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.

- 35,755
- 9
- 58
- 55
-
5Hi @olliej, is there any source for your statement? – Felipe Sabino Jan 03 '12 at 22:16
-
6@FelipeSabino Does working on a major engine and being on the ecmascript committee count? ;) More seriously though you can look at the sources for JavaScriptCore, SpiderMonkey, V8, etc online. – olliej Jan 06 '12 at 18:57
-
15Of course I could look at any open source code and check it for myself, but one of the reasons SO exists is to avoid this hassle, lol. It is not a question of doubting your knowledge, it is only a concern about helping developers with their research. It seems that you are someone that knows a lot about the subject, and also with much more thrustful references that could help me to learn a lot more about this subject. Just exemplifying, you said "in general strings are interned", what are the cases where they are not? and so on... – Felipe Sabino Jan 17 '12 at 14:16
-
7@FelipeSabino the logic for interning (at least in JSC) is spread over multiple areas. The basic model is similar to Java though -- constant strings are interned automatically, results of string concatenation, etc aren't. In Java you can explicitly force interning but that doesn't exist in JS. – olliej Feb 03 '12 at 05:38
-
1@olliej Could you please have a look on here - there is a strong debate http://stackoverflow.com/questions/26549715/why-javascript-string-interning-doesnt-give-constant-time-complexity-for also on here http://stackoverflow.com/questions/26532550/javascript-string-equality-performance-comparison/26532637#26532637. Thanks! Help is really appreciated :) – Michail Michailidis Oct 24 '14 at 15:20
http://jsperf.com/strinterning
Yes in Chrome, no in Aurora 15 and FF 13! Comparing two strings is 85% slower than comparing two pointers in Firefox. However it's the same speed in Chrome, which is an indication that it is comparing two pointers.
Maybe the JS engine team at Mozilla should check their code...

- 14,758
- 2
- 26
- 36

- 141
- 1
- 2
-
4If you think that's bad, IE9 doesn't even do pointer comparisons [when comparing a string variable to *itself*](http://www.kpozin.net/archive/2011/05/ie9-and-string-equality). ([Related JSPerfs](http://jsperf.com/string-comparison1/3).) – kpozin Jul 09 '12 at 03:12
-
Short answer: sometimes yes, sometimes no.
I also stumbled upon the same question and looked a bit into it. It seems that interning is done usually for string literals that are generated the same way (eg. always assigning the same string to a variable in the same loop), BUT I was also able to create an example which results in two identical strings being created with two different references:
As you can see, each string is stored twice, having different references.
This is the code I used to generate the duplicate strings:
const a = [];
const b = [];
for(let j =1; j<= 100;++j){
for(let i = 1; i <= 10000; ++i) a[i] = 'player 1 got 5 points from player 2' + i;
for(let i = 1; i <= 10000; ++i) b[i] = 'player 1 got 5 points from player 2' + i;
}
It seems that string interning is done for string literals, but not for concatenated string values, but as you can see above, each concatenated string only appears twice, not 100x2 = 200 times, so there is still string interning done for concatenated strings created in the outer loop.

- 1,237
- 1
- 12
- 20

- 27,244
- 26
- 101
- 151
-
what console is this? I can't get the arrows on the left to happen on strings, or more curiously the reference in grey on the right on opera/chrome/firefox – towc Feb 08 '19 at 07:55
-
-
3@XCS I don't think this is right: "there is still string interning done for concatenated strings created in the outer loop". You aren't maintaining references to the strings created in earlier iterations of the outer loop (every iteration you overwrite every element of `a` and `b`), but ["Only reachable objects are included in snapshots."](https://developer.chrome.com/docs/devtools/memory-problems/heap-snapshots/) If you [maintain references](https://output.jsbin.com/fuwibuk) to the earlier concatenated strings, then you [do get 200 copies](https://i.stack.imgur.com/nRRyd.png) of each string. – Han Seoul-Oh May 20 '21 at 19:58
-
-
@XCS I have no idea, unfortunately, that's why I was googling around and came upon this question! :) The only thing I'm sure of is that your test case does not illustrate a situation where concatenated strings are being interned, instead it illustrates that heap snapshots don't include unreachable objects. However, there may be other situations where concatenated strings _are_ interned, and I have no idea how to find one or prove they don't exist. – Han Seoul-Oh May 21 '21 at 21:58
-
@HanSeoul-Oh I don't think you can prove something doesn't exist, the only way would be to know how V8 works internally (or whatever JS engine we're testing this for). – XCS May 22 '21 at 18:04