Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compression of CoffeeScript output #315

Closed
mbarkhau opened this issue Oct 13, 2013 · 6 comments
Closed

Compression of CoffeeScript output #315

mbarkhau opened this issue Oct 13, 2013 · 6 comments

Comments

@mbarkhau
Copy link

I'm wondering if UglifyJS2 might not be able to do a better job at compressing common output of CoffeeScript. Here an example:

test = (
  (s) -> w.toLowerCase() for w in s.trim().split(" ")
)("test")

Which compiles to this:

var test;

test = (function(s) {
  var w, _i, _len, _ref, _results;
  _ref = s.trim().split(" ");
  _results = [];
  for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    w = _ref[_i];
    _results.push(w.toLowerCase());
  }
  return _results;
})("test");

Which is uglified to this:

var test;

test = function(t) {
    var e, r, s, n, o;
    for (n = t.trim().split(" "), o = [], r = 0, s = n.length; s > r; r++) e = n[r], 
    o.push(e.toLowerCase());
    return o;
}("test");

Shouldn't it also be possible to produce the following?

var test = function(t) {
    var n = t.trim().split(" "), o = [], r = 0, s = n.length;
    for (; s > r; r++) o.push(n[r].toLowerCase());
    return o;
}("test");

I removed the unnecessary temporary variable e from the last example, but mostly I'm referring to moving assignments up into the corresponding var declaration. I'm new to UglifyJS, so forgive me if I'm just missing a compiler flag.

@mbarkhau
Copy link
Author

Is this out of scope for UglifyJS?

@mishoo
Copy link
Owner

mishoo commented Oct 17, 2013

Nope, not out of scope but very hard to get right in the general case. A pull request is welcome. ;-)

@mbarkhau
Copy link
Author

Ok cool. I'll see what I can do.
Do you see any edge cases where this transformation might break code?

@mishoo
Copy link
Owner

mishoo commented Oct 17, 2013

Nope, absolutely not if the transformation is done properly. ;-) But it's hard. Good luck!

@ghost
Copy link

ghost commented Oct 20, 2013

I think that this seems relevant to #320.

@dead-claudia
Copy link

I would like to note that this is generally the case with the output of many compilers targeting JS. LiveScript, Coco, and to an extent, Emscripten all do this. Such a pull request will be very useful. (Minified JS and minified compiled CoffeeScript should be at least close in size, particularly in these situations.)

alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Apr 17, 2017
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue Apr 23, 2017
alexlamsl added a commit to alexlamsl/UglifyJS that referenced this issue May 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants