facebookarchive / prepack

A JavaScript bundle optimizer.
http://prepack.io
Other
14.21k stars 424 forks source link

Avoiding too many variables #2655

Open akidee opened 3 years ago

akidee commented 3 years ago

Input:

f = (function(a, b) {
  function id(s) {  return s; }
  return function m(c){
    let s = 0
    for (let i = 0; i < a; i++)
      s = s + c + id(b)
    return s
  }
})(4, 5)
__optimize(f);

Prepack output (with default settings in the REPL):

(function () {
  var _$8 = this;

  var _1 = function (c) {
    var _$0 = 0 + c;

    var _$1 = _$0 + 5;

    var _$2 = _$1 + c;

    var _$3 = _$2 + 5;

    var _$4 = _$3 + c;

    var _$5 = _$4 + 5;

    var _$6 = _$5 + c;

    var _$7 = _$6 + 5;

    return _$7;
  };

  _$8.f = _1;
}).call(this);

How can I avoid this many variables being defined?