facebookarchive/prepack

Eliminate branches in residual functions when the comparison value never leaks

Open

#1.146 geöffnet am 10. Nov. 2017

Auf GitHub ansehen
 (1 Kommentar) (0 Reaktionen) (0 zugewiesene Personen)JavaScript (520 Forks)batch import
bootcampedenhancementhelp wantedlevel 3 (medium)react compilerserializer

Repository-Metriken

Stars
 (14.268 Stars)
PR-Merge-Metriken
 (Keine gemergten PRs in 30 T)

Beschreibung

I'll just put this on the wish list.

(function() {
  const Foo = Symbol();
  const Bar = Symbol();
  reducer = function(action) {
    switch (action) {
      case Foo:
        console.log('foo');
        break;
      case Bar:
        console.log('bar');
        break;
    }
  };
  leak = Bar;
})();

should turn into:

(function () {
  var Bar = Symbol();
  reducer = function (action) {
    switch (action) {
      case Bar:
        console.log('bar');
        break;
    }
  };
  leak = Bar;
})();

Foo doesn't leak anywhere. Therefore it currently gets inlined to be created in the case statement producing a new Symbol every time. However, it should just dead code eliminate the whole conditional branch since it can never happen when the only remaining callsite is a condition.

Contributor Guide