Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

JavaScript string type is immutable, but does it mean identical strings will share memory?

Say two strings are created in complete different places, will a modern JavaScript engine be smart enough to not double the amount of memory for each "copy" and just re-use the one already created (that exists somewhere in memory)?

Say we got:

var foo = sha256(fooInput); 
//  ... somewhere else
var bar = sha256(barInput);

// Say foo and bar is identical: "foo === bar" => true
// Would they share memory? 

Does it make any difference if the string value is a key in objects? Example:

var cars = {
  "john": {
    // information about all cars "john" owns.
  }
};

var computers = {
  "john": {
    // information about all computers "john" owns.
  }
};

var phones = {
  "john": {
    // information about all phones "john" owns.
  }
};

Will the string "john" in the example above take up memory of three or one string of "john"?

share|improve this question
up vote 1 down vote accepted

The process of sharing strings in this manner is called String Interning, and yes, Javascript does it. The way in which string interning is accomplished in Javascript is an implementation detail, and it varies between different Javascript implementations.

From a performance perspective, you probably shouldn't be worrying about this unless it becomes a significant problem affecting your application that you have specifically identified with a profiling tool.

share|improve this answer
    
Thank you Robert :-) – Fredrik Jul 26 at 17:34
    
Nitpick: if it's an implementation detail and varies between different implementations, then, no JavaScript doesn't do it. Some JavaScript implementations may or may not do it to some degree or other, but there is no guarantee. This is like saying that "Java does TCO, it's an implementation detail, and it varies between different implementations, but yes, Java does it." – Jörg W Mittag Jul 27 at 0:19
    
@JörgWMittag: Seems like a distinction without a difference. – Robert Harvey Jul 27 at 14:50
    
Personally, I believe the distinction between a language feature that is guaranteed by the language spec, and a private internal implementation detail of an implementation that may change any time without notice and may or may not exist on other implementations, and if it even exists at all may or may not behave the way you expect, is a big difference. Especially in JavaScript, where you have pretty much no control over the implementation that your code is going to run in. – Jörg W Mittag Jul 27 at 16:01
    
@JörgWMittag: I guess my point is that only software developers would see my "yes, Javascript does this" statement, and interpret it as a mandate. If most, or all, implementations do it, then Javascript most certainly does it. It may not do it on a train, it may not do it on a plane, but Javascript does exhibit this behavior in some form in all of its implementations. – Robert Harvey Jul 27 at 16:02

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.