2

In some languages, frequently used strings are defined as variables/constants, which are called instead of literal strings. Is this the same with JavaScript? In particular, I have frequent use of the string 'none'. Instead of writing the literal 'none' everywhere in the code, would it improve performance if I define:

var none = 'none';

and use none everywhere in the code? Or, is there a way to intern a literal string expression so that it is evaluated only once?

sawa
  • 165,429
  • 45
  • 277
  • 381
  • 1
    Storing the string in a variable will reduce the number of bytes used by the source code itself. In this respect, your code could be executed faster. – LuvnJesus Feb 08 '16 at 00:13
  • 2
    Have you profiled your code? Are you sure this kind of micro-optimization is really going to help? – Sam Axe Feb 08 '16 at 00:13
  • 2
    And if you use a variable, and change the string, you only have one place to look for a typo (assuming you initially checked and verified the variable names). As for "*does it improve performance*" – go to [JS Perf](http://jsperf.com) and check for yourself. – David Thomas Feb 08 '16 at 00:14
  • @SamAxe I am not sure at all. That is why I am asking. – sawa Feb 08 '16 at 00:14
  • 1
    "In some languages, frequently used strings are defined as variables/constants, which are called instead of literal strings" --- like in what languages? It sounds odd and useless. – zerkms Feb 08 '16 at 00:16
  • @zerkms At least in Ruby and LaTeX (which are among the few languages that I know), it is a common practice. – sawa Feb 08 '16 at 00:18
  • How about java, c(++), c#? – zerkms Feb 08 '16 at 00:19
  • 1
    This is both the kind of micro-optimization that has absolutely no relevance in any practical software project, and the kind of thing that's trivial for the interpreter to optimize automatically. – JJJ Feb 08 '16 at 00:19
  • @Juhana it's not an optimisation, even micro. Mature compilers optimise it more effeciently. – zerkms Feb 08 '16 at 00:19
  • I am not claiming that this is a good practice. (I have no idea why any of you are thinking that I am.) I am asking whether it is. If any of you have a concrete answer, then please provide an answer. – sawa Feb 08 '16 at 00:22
  • Stackoverflow is not about opinions - there is a close-vote reason for that. Your question has nothing to do with performance. Those projects define a string in a shared place so that it could be changed easily. – zerkms Feb 08 '16 at 00:23
  • @zerkms You are absolutely wrong. LaTeX even defines something like (I forgot how it exactly was) `\two` for `2` (don't blame me if it is not exact), whose sole purpose is performance, and does not help in any way for organization. In Ruby, a way to describe interned strings was introduced to free people from doing that. – sawa Feb 08 '16 at 00:27
  • 1
    @sawa so, if it supports string interning, what is the point to define a global variable? – zerkms Feb 08 '16 at 00:33
  • @zerkms As I wrote, prior to that, people had to define a variable. The purpose of interning is to free people from doing it anymore. – sawa Feb 08 '16 at 00:33
  • Note that using a common variable *everywhere* that might previously have had the string literal 'none' implies to anyone reading the code that that value has the same purpose everywhere. Which it might - but it might not: 'none' could in some places be used for setting CSS properties, but in other places be used in constructing error messages, and in other places be used for something else again. If you are going to use a variable, you should (in my example) have one variable for the CSS usages, another variable for the error messages, etc. – nnnnnn Feb 08 '16 at 01:17

1 Answers1

2

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');
pedrotp
  • 308
  • 1
  • 7
  • "automatically interned in Javascript" --- this phrase need to be changed: the ES standard does not even define string interning. So it's an *optional* property of an implementation. – zerkms Feb 08 '16 at 00:28
  • "The only time Javascript creates different string objects for the same string value is if you define each one via the String global object" --- how do you know the underlying data is not interned? What your assumption is based on? – zerkms Feb 08 '16 at 00:30
  • After rewording it's still not obvious why you think `hello` string literal cannot be interned. – zerkms Feb 08 '16 at 00:32
  • @Pedro Thanks for the help. It seems you are correct. – sawa Feb 08 '16 at 00:41
  • @sawa "It seems you are correct" -- how do you know that? This answer provides zero references anywhere. Software engineering is about dealing with facts, not trusting someone in the internet. – zerkms Feb 08 '16 at 00:47
  • @zerkms At least, I don't trust you. – sawa Feb 08 '16 at 00:48
  • The example with `new String()` would indeed create separate string objects, but the two string literals used to create them could still be interned by the JS engine. But for practical purposes when would a program ever need to check if two string objects are the same object as compared to having the same .toString() value? I've *never* needed to use `new String()`. – nnnnnn Feb 08 '16 at 01:24