babel / babel/minify

Temp Variable Inlining

Open
#498 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
4.4k
Forks
217
PR merge metrics
No merged PRs in 30d

Description

assumption: all function calls cause side-effects and order matters.

**Number of impure expressions**

If there are more than 1 impure expression, then optimize if and ONLY if the order is maintained - between and including the binding declaration and the last usage of the variable.

``` js
function foo() {
let x = Foo(),
y = Bar();
return x;
}
function bar() {
let x = Foo();
return Bar(x); // order is maintained. So can replace.
}
function foo1() {
let a = Foo(), b = Bar();
return b + a; // order changes
}
function foo2() {
let a = Foo();
return Bar(Baz() + a); // order changes
}
```

**Async Callbacks**

``` js
function foo() {
var x = foo();
return new Promise((resolve, reject) => resolve(x));
}
function bar() {
var x = foo();
callAsync(() => x);
}
```

**Sync callbacks and Loops**

``` js
function foo() {
let x = foo();
return items.map(() => x);
}
function bar() {
let x = foo();
for (;;) {
if (a) bar(x);
}
}
```

**Side effecty getters and setters**
- `pure_getters` as an option - where we need not count getters as impure expression


``` js
function foo() {
let x = bar.foo();
bar.baz(x);
}
function foo2() {
let x = { foo: foo(), bar: bar() };
module.exports = x; // order doesn't change. can replace
}
function foo3() {
let x = foo();
module.foo.exports = x; // order changes because of getter.
}
```

**Chain replacements**

If we are doing this, we should chain them

``` js
function baz() {
let x = Foo(),
y = Bar(x),
z = Baz(y);
return z; // order is maintained
}
```

**Destructuring**

Right now this is disabled in DCE. May be we can use the `pure_getters` option here as well

``` js
function foo() {
let {x} = a();
return x; // can be optimized to a().x
}
// but should bail when
function bar() {
let {x, y} = a();
return x;
}
```

Probably array destructuring will be shorter with a temp variable than using the iterator protocol ?

> copied from https://github.com/babel/babili/pull/221#issuecomment-260615569

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.