Skip to content

Commit

Permalink
improve compress granularity through typeofs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Jul 4, 2017
1 parent 5f046c7 commit 162b77f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,9 @@ If you're using the `X-SourceMap` header instead, you can just omit `sourceMap.u
- `booleans` -- various optimizations for boolean context, for example `!!a
? b : c → a ? b : c`

- `typeofs` -- `typeof foo == "undefined"``foo === void 0`
Note: Internet Explorer 10 or below has known issue with special values of `foo`.

- `loops` -- optimizations for `do`, `while` and `for` loops when we can
statically determine the condition

Expand Down Expand Up @@ -873,7 +876,6 @@ when this flag is on:
- `new Object()``{}`
- `String(exp)` or `exp.toString()``"" + exp`
- `new Object/RegExp/Function/Error/Array (...)` → we discard the `new`
- `typeof foo == "undefined"``foo === void 0`
- `void 0``undefined` (if there is a variable named "undefined" in
scope; we do it because the variable name will be mangled, typically
reduced to a single character)
Expand Down Expand Up @@ -1050,5 +1052,5 @@ uglifyjs file.js -m
```
To enable fast minify mode with the API use:
```js
UglifyJS.minify(code, { compress: false, mangle: true });
UglifyJS.minify(code, { compress: false, mangle: true });
```
4 changes: 3 additions & 1 deletion lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function Compressor(options, false_by_default) {
switches : !false_by_default,
top_retain : null,
toplevel : !!(options && options["top_retain"]),
typeofs : !false_by_default,
unsafe : false,
unsafe_comps : false,
unsafe_Func : false,
Expand Down Expand Up @@ -3591,7 +3592,8 @@ merge(Compressor.prototype, {
case "==":
case "!=":
// "undefined" == typeof x => undefined === x
if (self.left instanceof AST_String
if (compressor.option("typeofs")
&& self.left instanceof AST_String
&& self.left.value == "undefined"
&& self.right instanceof AST_UnaryPrefix
&& self.right.operator == "typeof") {
Expand Down
10 changes: 7 additions & 3 deletions test/compress/issue-1446.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
typeof_eq_undefined: {
options = {
comparisons: true
comparisons: true,
typeofs: true,
}
input: {
var a = typeof b != "undefined";
Expand All @@ -24,6 +25,7 @@ typeof_eq_undefined_ie8: {
options = {
comparisons: true,
ie8: true,
typeofs: true,
}
input: {
var a = typeof b != "undefined";
Expand All @@ -45,7 +47,8 @@ typeof_eq_undefined_ie8: {

undefined_redefined: {
options = {
comparisons: true
comparisons: true,
typeofs: true,
}
input: {
function f(undefined) {
Expand All @@ -58,7 +61,8 @@ undefined_redefined: {

undefined_redefined_mangle: {
options = {
comparisons: true
comparisons: true,
typeofs: true,
}
mangle = {}
input: {
Expand Down
2 changes: 2 additions & 0 deletions test/compress/reduce_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,7 @@ issue_1670_1: {
reduce_vars: true,
side_effects: true,
switches: true,
typeofs: true,
unused: true,
}
input: {
Expand Down Expand Up @@ -1532,6 +1533,7 @@ issue_1670_3: {
reduce_vars: true,
side_effects: true,
switches: true,
typeofs: true,
unused: true,
}
input: {
Expand Down

0 comments on commit 162b77f

Please sign in to comment.