Live data from Hacker News

Closure Compiler: a tool for making JavaScript download and run faster

developers.google.com

11–20 of 77 posts

Re: Closure Compiler: a tool for making JavaScript download and run faster

#11
post #2

How can it determine dead code for such a dynamic language like JavaScript? Is it limited to obvious stuff like things after a "return;" statement that cannot run? Because I don't see how it could remove unused functions when you can invoke them in such indirect ways: myobj['f'+'oo']();

    function add(a,b) { return a+b }
    console.log(add(1,2))
Compiles to

    console.log(3)
I'm not sure about your example, but I think it would work. (Apparently not)

Re: Closure Compiler: a tool for making JavaScript download and run faster

#13
post #2

How can it determine dead code for such a dynamic language like JavaScript? Is it limited to obvious stuff like things after a "return;" statement that cannot run? Because I don't see how it could remove unused functions when you can invoke them in such indirect ways: myobj['f'+'oo']();

Have a look at the advanced compilation option: https://developers.google.com/closure/compiler/docs/api-tuto...

Basically when enabled it will rewrite normal property access, e.g: `myObj.foo` to something like `a.b` but wont shorten string literal access, e.g `myObj['foo']` just becomes `a.foo` which you would use for any API's you want to export.

For a real-world example I use string literals for declaring jquip's public API:

    p['removeAttr'] = function(name){
        return this['each'](function(){
          if (this.nodeType == 1) this.removeAttribute(name);
        });
    };
Which closure advanced rewrites to:

    q.removeAttr=function(a){return this.each(function(){this.removeAttribute(a)})};
Original: https://github.com/mythz/jquip/blob/master/dist/jquip.all.js

Compiled: https://github.com/mythz/jquip/blob/master/dist/jquip.all.cl...

Re: Closure Compiler: a tool for making JavaScript download and run faster

#15
post #8
post #4

The title of this submission omits the best part; closure provides static type checking. That is more valuable to me than the dead code removal & minification. I just discovered an amazing and nearly undocumented feature of the Closure Compiler: the --module flag. One of the most inconvenient parts of using closure is having to compile each page separately. That's what the Closure docs say to do: https://developers.g…

I think it's likely that long-term, the approach FB took with Flow (or more general Abstract Interpretation methods) should be able to handle more cases with less effort on the developer's side (albeit with increased computational costs). The modules/"code motion" is a bit strange at first glance, but definitely a huge boon for building debuggers/admin interfaces/etc. that you don't want included in your main applica…

It's not clear what you're referring to wrt. "the approach FB took with Flow" and Abstract Interpretation. Type checking? Dead code elimination? In anycase, Google Closure Compiler also leverages Abstract Interpretation.

Re: Closure Compiler: a tool for making JavaScript download and run faster

#16
post #13
post #2

How can it determine dead code for such a dynamic language like JavaScript? Is it limited to obvious stuff like things after a "return;" statement that cannot run? Because I don't see how it could remove unused functions when you can invoke them in such indirect ways: myobj['f'+'oo']();

Have a look at the advanced compilation option: https://developers.google.com/closure/compiler/docs/api-tuto... Basically when enabled it will rewrite normal property access, e.g: `myObj.foo` to something like `a.b` but wont shorten string literal access, e.g `myObj['foo']` just becomes `a.foo` which you would use for any API's you want to export. For a real-world example I use string literals for declaring jquip's p…

May i ask why you are using this notation? I honestly can't think of any benefits. Isn't it more convenient and readable to use `p.removeAttr`?

Re: Closure Compiler: a tool for making JavaScript download and run faster

#19
post #14

I keep trying to use closure on my projects but it doesn't compile jQuery to working code. I always get errors after concatenating and using it on my sources and this always just brings me back to uglify.

https://code.google.com/p/closure-compiler/wiki/ExternsForCo...

Re: Closure Compiler: a tool for making JavaScript download and run faster

#20
post #7
post #4

The title of this submission omits the best part; closure provides static type checking. That is more valuable to me than the dead code removal & minification. I just discovered an amazing and nearly undocumented feature of the Closure Compiler: the --module flag. One of the most inconvenient parts of using closure is having to compile each page separately. That's what the Closure docs say to do: https://developers.g…

In ClojureScript there is a attempted going to make Closure Compiler Modules work with other modules: https://github.com/clojure/clojurescript/wiki/Google-Summer-...

The Google Summer of Code project is unrelated to code splitting which is what --modules is referring to in this context. The ClojureScript GSoC project was about integrating the variety of JS module formats (CommonJS, AMD, ES2015) found in the wild as well as supporting preprocessing phases (React's JSX).
Post reply on HN