Valuable savings (a.k.a. String constants)

Has any one thought of this before ? I actually think this is a “good thing”.

Here I am talking about JavaScript as a stand alone programming language. Not about browser+JavaScript.

String constants. This is the feature, I would like to have (in the JavaScript language) :

Now imagine the space savings if You use this everywhere in your code ? Sizeable, but not dramatic.
And now imagine the savings if this is part of the ECMA 5?
Now, that just might be something we might call: “valuable savings” … The more we use it, the more we save.

Also.If this is part of the language (ECMA5) all of the string literals, lurking around JavaScript , should be made into an:

With the added benefit of them actually being constants. And yes, including this one too :

One non-trivial achievement for JavaScript. And a good thing for the wwww1. If this is made part of ECMA5, then everyone will be using this everywhere. Thus, the total size of HTTP traffic on the www will be measurably smaller. Is this just my speculation? I dare to think, not.

Update 2009 08 12

String literals are bad. Not having constants in a language is bad. Having string literals and no constants, is even worse. What are the constants for is well known. Imagine if Math.PI would not be a constant ? VBScript has contants2. String literals are always gouped in “one place” . Other platforms (.net, java) have resource files where string dictionaries are kept . Transforming “official” string literals into string constants, it the least we can do for the JavaScript. Especially these days when it tryes to grow-up with the help of ECMA comitee.

Update 2009 08 13

Jacke Archibald “made me” add this important clarification.
Before minimization there are no savings. After minimaztion, in the presence of global constants there are sizeable savings. String literlas can not be compressed, while a constant String.jacke // defined as: "Jacke Archibald" ; can be effectively compressed . This is also yet another convincing example on why string literals are bad.

Update 2009 08 14

I can present an workable solution for this straight away. I have found this interesting “compressor” here: It has an interesting (and unique?) feature of understandning #define commands. Example :

Is transformed into:

There are other commands like #if #else #endif etc . Of course #defines are hidden in /* */ comments. This compressor seems an ideal candidate to implement String constants support, before ECMA comitee does:

The rest of the code, now can use String constants for improved maintainability, changeability and less bugs.

After the transformation through the “compression”, the result is :

Above will work and will bring all the benefits of String properties (and other) as global constants. Until we have ECMA Script which has these constants built in.
After which event, we do the same compression technique, without our global constants. Looks a (bit too) complicated. For large javascript web applications this would be a very important feature to improve the code quality and app stability. With the help of the (ever important) code compression. Also, after global constants are part of the language, compressors can use the same technique, but transparently to the users.

1: “whole www” a.k.a. “wwww”
2: There are JavaScript “solutions” which are using VBScript to “implement” constants in IE+JScript.

11 thoughts on “Valuable savings (a.k.a. String constants)”

Comments are closed.