Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
up vote 12 down vote accepted

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.

share|improve this answer
    
Hi @olliej, is there any source for your statement? – Felipe Sabino Jan 3 '12 at 22:16
2  
@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 6 '12 at 18:57
1  
Of 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
3  
@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 3 '12 at 5:38
    
@olliej Could you please have a look on here - there is a strong debate stackoverflow.com/questions/26549715/… also on here stackoverflow.com/questions/26532550/…. 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...

share|improve this answer
2  
If you think that's bad, IE9 doesn't even do pointer comparisons when comparing a string variable to itself. (Related JSPerfs.) – kpozin Jul 9 '12 at 3:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.