Do common JavaScript implementations use string interning?
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.
Does defining a frequently used string as a variable improve performance?
Literal strings are automatically interned by most Javascript compilers. So var a = 'hello'
and var b = 'hello'
will likely already be pointing at the same copy of the 'hello'
string in memory, no need for further optimization on your part.
The only way to make sure different string objects are created for the same string value is by defining each one via the String global object, i.e.:
var a = new String('hello');
var b = new String('hello');
Does Web Worker postMessage benefit from string interning?
The worker and the main thread definitely have at least one copy each, because their heaps are isolated from each other. The message itself does not use de-duplication for strings, so it will include several copies of the character data. The receiving side may apply interning later on, depending on what you do with the strings there. (The same is true for the sending side, or for apps that don't involve postMessage
at all -- not all strings are interned because interning comes at a cost too, so it's easy to create a situation where you have several copies of the same string.)
That said: all of this is an implementation detail. It may well change over time. So this answer might become outdated in the future.
Do common JavaScript implementations use string interning?
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.
Is it good practice to use java.lang.String.intern()?
When would I use this function in favor to String.equals()
when you need speed since you can compare strings by reference (== is faster than equals)
Are there side effects not mentioned in the Javadoc?
The primary disadvantage is that you have to remember to make sure that you actually do intern() all of the strings that you're going to compare. It's easy to forget to intern() all strings and then you can get confusingly incorrect results. Also, for everyone's sake, please be sure to very clearly document that you're relying on the strings being internalized.
The second disadvantage if you decide to internalize strings is that the intern() method is relatively expensive. It has to manage the pool of unique strings so it does a fair bit of work (even if the string has already been internalized). So, be careful in your code design so that you e.g., intern() all appropriate strings on input so you don't have to worry about it anymore.
(from JGuru)
Third disadvantage (Java 7 or less only): interned Strings live in PermGen space, which is usually quite small; you may run into an OutOfMemoryError with plenty of free heap space.
(from Michael Borgwardt)
Why Javascript ===/== string equality sometimes has constant time complexity and sometimes has linear time complexity?
Based on all the Performance Tests (see original post) for strings a and b the operation a === b
takes:
constant time O(1) if the strings are interned. From the examples it seems that interning only happens with directly assigned strings like
var str = "test";
and not if you build it with concatenation using for-loops or functions.linear time O(N) since in all the other cases the length of the two strings is compared first. If it is equal then we have character by character comparison. Else of course they are not equal. N is the length of the string.
Do common JavaScript implementations use string interning?
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.
Related Topics
How to Get the File Name from a Full Path Using JavaScript
How to Focus on a <Div> Using JavaScript Focus() Function
How to Find Prime Numbers Between 0 - 100
How to Filter Multiple Values (Or Operation) in Angularjs
How to Use Jquery for Xml Parsing with Namespaces
Adding Click Event Listener to Elements with the Same Class
Over_Query_Limit in Google Maps API V3: How to Pause/Delay in JavaScript to Slow It Down
Required Field Validations Not Working in Jquery Popup MVC 4
Are There Constants in JavaScript
Detecting Arrow Key Presses in JavaScript
Render Partial View Using Jquery in ASP.NET MVC
Where Do You Include the Jquery Library From? Google JSAPI? CDN
Define Global Variable with Webpack
Execute the Setinterval Function Without Delay the First Time
Insert HTML at Caret in a Contenteditable Div
How to Edit a JavaScript Alert Box Title
Why Is My Function Call That Should Be Scheduled by Settimeout Executed Immediately