Skip to content

Compression of CoffeeScript output #315

Closed

Description

@mbarkhau

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.

Activity

  1. mbarkhau commented on Oct 17, 2013

    @mbarkhau
    Author

    Is this out of scope for UglifyJS?

  2. mishoo commented on Oct 17, 2013

    @mishoo
    Owner

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

  3. mbarkhau commented on Oct 17, 2013

    @mbarkhau
    Author

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

  4. mishoo commented on Oct 17, 2013

    @mishoo
    Owner

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

  5. dead-claudia commented on Jan 25, 2015

    @dead-claudia

    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.)

  6. added 2 commits that reference this issue on Apr 17, 2017
    2126333
    65a7d92
  7. added a commit that references this issue on May 3, 2017
    2539db0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions