Suggestion: constants for strings

24 views
Skip to first unread message

cjavapro

unread,
Feb 7, 2011, 6:23:08 PM2/7/11
to Closure Compiler Discuss
If a string shows up often then you can alias it

For example if someone uses jquery and the jquery externs file and you
use the advanced compile option then you may find frequent calls to $
('body') or $('#someselector'). Those strings can be saved to one
letter variables which would shorten the code very much.

Granted you may want this to be an additional feature or custom switch
that is not on by default because of possible performance
implications.

Also see this which may want to be part of that same switch. Perhaps
call it WAY_ADVANCED_OPTIMIZATIONS.
https://groups.google.com/group/closure-compiler-discuss/browse_thread/thread/debaf42b12fc926d

Nick Santos

unread,
Feb 7, 2011, 6:32:27 PM2/7/11
to closure-comp...@googlegroups.com
This is already available via the Java API. It's called aliasAllStrings.
http://code.google.com/p/closure-compiler/wiki/FAQ#Closure_Compiler_inlined_all_my_strings,_which_made_my_code_size

As the above link explains, it isn't generally useful. gzip can
compress the common strings much better than we possibly can, because
gzip is not limited to JS.

cjavapro

unread,
Feb 7, 2011, 6:36:12 PM2/7/11
to Closure Compiler Discuss
Are you saying that gzip looks for strings that occur frequently for a
given file? or is it just that it substitutes a lot of common english?

On Feb 7, 6:32 pm, Nick Santos <nicksan...@google.com> wrote:
> This is already available via the Java API. It's called aliasAllStrings.http://code.google.com/p/closure-compiler/wiki/FAQ#Closure_Compiler_i...
>
> As the above link explains, it isn't generally useful. gzip can
> compress the common strings much better than we possibly can, because
> gzip is not limited to JS.
>
>
>
>
>
>
>
> On Mon, Feb 7, 2011 at 6:23 PM, cjavapro <cjava...@gmail.com> wrote:
> > If a string shows up often then you can alias it
>
> > For example if someone uses jquery and the jquery externs file and you
> > use the advanced compile option then you may find frequent calls to $
> > ('body') or $('#someselector'). Those strings can be saved to one
> > letter variables which would shorten the code very much.
>
> > Granted you may want this to be an additional feature or custom switch
> > that is not on by default because of possible performance
> > implications.
>
> > Also see this which may want to be part of that same switch. Perhaps
> > call it WAY_ADVANCED_OPTIMIZATIONS.
> >https://groups.google.com/group/closure-compiler-discuss/browse_threa...

Nick Santos

unread,
Feb 7, 2011, 6:49:21 PM2/7/11
to closure-comp...@googlegroups.com
gzip is based on DEFLATE. Wikipedia has a very detailed desctiption of it.
http://en.wikipedia.org/wiki/DEFLATE
At a high-level, the algorithm just looks for common sequences of
bytes, and creates aliases for them. So it can intelligently alias
substrings, or entire strings, or even the JS syntax around the string
(like parentheses).

cjavapro

unread,
Feb 7, 2011, 7:10:31 PM2/7/11
to Closure Compiler Discuss
COOL! Thanks! I would have never guess it if I was writing my own
JavaScript compressor! Nice having Googe Engineers around :)

On Feb 7, 6:49 pm, Nick Santos <nicksan...@google.com> wrote:
> gzip is based on DEFLATE. Wikipedia has a very detailed desctiption of it.http://en.wikipedia.org/wiki/DEFLATE
Reply all
Reply to author
Forward
0 new messages