Do common JavaScript implementations use string interning?

Asked
Active3 hr before
Viewed126 times

6 Answers

commonjavascript
90%

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.,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?,Find centralized, trusted content and collaborate around the technologies you use most.,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.

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;
}
88%

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?,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.,As you can see, each string is stored twice, having different references.,This is the code I used to generate the duplicate strings:

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;
}
72%

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?,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.,Micro-optimisation benefits of interning strings manually are minimal hence is generally not recommended.,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.

This probably describes it:

class Program {
   const string SomeString = "Some String"; // gets interned

   static void Main(string[] args) {
      var s1 = SomeString; // use interned string
      var s2 = SomeString; // use interned string
      var s = "String";
      var s3 = "Some " + s; // no interning 

      Console.WriteLine(s1 == s2); // uses interning comparison
      Console.WriteLine(s1 == s3); // do NOT use interning comparison
   }
}
load more v
65%

3.2 Reclaiming unused interned strings,In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable.[1] Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool. ,Guava Java Library - Interner - Non-permgen String.intern and supports other immutable types with weak and strong referenced implementations,The single copy of each string is called its intern and is typically looked up by a method of the string class, for example String.intern()[2] in Java. All compile-time constant strings in Java are automatically interned using this method.[3]

String interning is supported by some modern object-oriented programming languages, including Java, Python, PHP (since 5.4), Lua,[4] Ruby (with its symbols), Julia and .NET languages.[5] Lisp, Scheme, and Smalltalk are among the languages with a symbol type that are basically interned strings. The library of the Standard ML of New Jersey contains an atom type that does the same thing. Objective-C's selectors, which are mainly used as method names, are interned strings.

Objects other than strings can be interned. For example, in Java, when primitive values are boxed into a wrapper object, certain values (any boolean, any byte, any char from 0 to 127, and any short or int between −128 and 127) are interned, and any two boxing conversions of one of these values are guaranteed to result in the same object.[6]

boolean

String interning is supported by some modern object-oriented programming languages, including Java, Python, PHP (since 5.4), Lua,[4] Ruby (with its symbols), Julia and .NET languages.[5] Lisp, Scheme, and Smalltalk are among the languages with a symbol type that are basically interned strings. The library of the Standard ML of New Jersey contains an atom type that does the same thing. Objective-C's selectors, which are mainly used as method names, are interned strings.

Objects other than strings can be interned. For example, in Java, when primitive values are boxed into a wrapper object, certain values (any boolean, any byte, any char from 0 to 127, and any short or int between −128 and 127) are interned, and any two boxing conversions of one of these values are guaranteed to result in the same object.[6]

byte

String interning is supported by some modern object-oriented programming languages, including Java, Python, PHP (since 5.4), Lua,[4] Ruby (with its symbols), Julia and .NET languages.[5] Lisp, Scheme, and Smalltalk are among the languages with a symbol type that are basically interned strings. The library of the Standard ML of New Jersey contains an atom type that does the same thing. Objective-C's selectors, which are mainly used as method names, are interned strings.

Objects other than strings can be interned. For example, in Java, when primitive values are boxed into a wrapper object, certain values (any boolean, any byte, any char from 0 to 127, and any short or int between −128 and 127) are interned, and any two boxing conversions of one of these values are guaranteed to result in the same object.[6]

char

String interning is supported by some modern object-oriented programming languages, including Java, Python, PHP (since 5.4), Lua,[4] Ruby (with its symbols), Julia and .NET languages.[5] Lisp, Scheme, and Smalltalk are among the languages with a symbol type that are basically interned strings. The library of the Standard ML of New Jersey contains an atom type that does the same thing. Objective-C's selectors, which are mainly used as method names, are interned strings.

Objects other than strings can be interned. For example, in Java, when primitive values are boxed into a wrapper object, certain values (any boolean, any byte, any char from 0 to 127, and any short or int between −128 and 127) are interned, and any two boxing conversions of one of these values are guaranteed to result in the same object.[6]

short

String interning is supported by some modern object-oriented programming languages, including Java, Python, PHP (since 5.4), Lua,[4] Ruby (with its symbols), Julia and .NET languages.[5] Lisp, Scheme, and Smalltalk are among the languages with a symbol type that are basically interned strings. The library of the Standard ML of New Jersey contains an atom type that does the same thing. Objective-C's selectors, which are mainly used as method names, are interned strings.

Objects other than strings can be interned. For example, in Java, when primitive values are boxed into a wrapper object, certain values (any boolean, any byte, any char from 0 to 127, and any short or int between −128 and 127) are interned, and any two boxing conversions of one of these values are guaranteed to result in the same object.[6]

int
load more v
75%

html - JavaScript string / integer comparison ,But I'm afraid that when I compare them using if (myPlanet === Planet.Earth), the string comparison could take a lot longer (say if it were in a tight loop). This should be the case because http://ecma-international.org/ecma-262/5.1/#sec-11.9.6 says, Comparison string JavaScript may return% of ,I know this could be a micro optimization, but my question is: is string equality comparison done using pointers and therefore just as fast as number equality comparison?

For example

// I don't want this because when debugging, you'd see just the value 0
var Planets = {
   Earth: 0,
   Mars: 1,
   Venus: 2
}

// I'd prefer this so that Planets.Earth gives me a nice readable value ("Earth")
var Planets = {
   Earth: 'Earth',
   Mars: 'Mars'
}
40%

substringReturns a string containing a subsequence of characters from this string. The returned string shares,lengthReturns the number of characters in this string., A pool of strings, initially empty, is maintained privately by the class String. ,splitSplits this string using the supplied regularExpression. See Pattern#split(CharSequence,int) for an

private void myMethod() {
   Gson g = new Gson() GsonBuilder gsonBuilder;
   gsonBuilder.create() new GsonBuilder().create() Smart code suggestions by Tabnine
}
load more v

Other "common-javascript" queries related to "Do common JavaScript implementations use string interning?"