babel / babel/minify

Smart constant rewriting

Open
#156 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

(as suggested by @sebmarkbage)

The minifier already has simple constant folding and/or dead-code elimination of primitives:

``` js
if (__DEV__) {
}
```

It would be great if it could also do that for frozen objects and non-writable/non-configurable properties.

If I declare an object like:

``` js
var obj = {};
Object.defineProperty(obj, 'foo', { configurable: false, writable: false, value: false });
```

Then this expression should also evaluate to false and be dead-code eliminated:

``` js
if (obj.foo) {
}
```

If the primitive value is a number or boolean it would rewrite it:

``` js
var x = obj.foo;
```

could be rewritten to:

``` js
var x = 123;
```

However, the most important one for potential further optimization would be rewriting bindings for non-primitives;

A binding like:

``` js
obj.foo();
```

would be rewritten to:

``` js
foo();
```

and the minifier would somehow communicate back that the obj binding is now replaced with the foo binding which should have the value of `obj.foo`.

Contributor guide

Open the contributing guide

Research direction

The issue names no files or tests. Start by tracing the minifier’s existing primitive constant-folding and dead-code-elimination paths, then compare them with the frozen-object and non-writable/non-configurable property examples. Done would require a clear scope for constant rewriting, dead-branch elimination, and non-primitive binding replacement.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.