Skip to content

Commit

Permalink
feat(minifier): fold typeof foo != "undefined" into `typeof foo < "…
Browse files Browse the repository at this point in the history
…u"` (#8159)
  • Loading branch information
sapphi-red authored Dec 28, 2024
1 parent 65796c4 commit f3a36e1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,24 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
}

/// Compress `typeof foo == "undefined"` into `typeof foo > "u"`
///
/// - `typeof foo == "undefined"` -> `typeof foo > "u"`
/// - `typeof foo != "undefined"` -> `typeof foo < "u"`
///
/// Enabled by `compress.typeofs`
fn try_compress_typeof_undefined(
expr: &mut BinaryExpression<'a>,
ctx: Ctx<'a, 'b>,
) -> Option<Expression<'a>> {
if !matches!(expr.operator, BinaryOperator::Equality | BinaryOperator::StrictEquality) {
return None;
}
let new_op = match expr.operator {
BinaryOperator::Equality | BinaryOperator::StrictEquality => {
BinaryOperator::GreaterThan
}
BinaryOperator::Inequality | BinaryOperator::StrictInequality => {
BinaryOperator::LessThan
}
_ => return None,
};
let pair = Self::commutative_pair(
(&expr.left, &expr.right),
|a| a.is_specific_string_literal("undefined").then_some(()),
Expand All @@ -266,7 +276,7 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
let argument = Expression::Identifier(ctx.alloc(id_ref));
let left = ctx.ast.expression_unary(SPAN, UnaryOperator::Typeof, argument);
let right = ctx.ast.expression_string_literal(SPAN, "u", None);
Some(ctx.ast.expression_binary(expr.span, left, BinaryOperator::GreaterThan, right))
Some(ctx.ast.expression_binary(expr.span, left, new_op, right))
}

/// Compress `foo === null || foo === undefined` into `foo == null`.
Expand Down Expand Up @@ -1143,6 +1153,20 @@ mod test {
test_same("const foo = () => { foo; return 'baz' }");
}

/// Port from <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_parser/js_parser_test.go#L4658>
#[test]
fn test_fold_is_typeof_equals_undefined() {
test("typeof x !== 'undefined'", "typeof x < 'u'");
test("typeof x != 'undefined'", "typeof x < 'u'");
test("'undefined' !== typeof x", "typeof x < 'u'");
test("'undefined' != typeof x", "typeof x < 'u'");

test("typeof x === 'undefined'", "typeof x > 'u'");
test("typeof x == 'undefined'", "typeof x > 'u'");
test("'undefined' === typeof x", "typeof x > 'u'");
test("'undefined' == typeof x", "typeof x > 'u'");
}

#[test]
fn test_fold_is_null_or_undefined() {
test("foo === null || foo === undefined", "foo == null");
Expand Down
20 changes: 10 additions & 10 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ Original | minified | minified | gzip | gzip | Fixture
-------------------------------------------------------------------------------------
72.14 kB | 23.74 kB | 23.70 kB | 8.61 kB | 8.54 kB | react.development.js

173.90 kB | 60.22 kB | 59.82 kB | 19.49 kB | 19.33 kB | moment.js
173.90 kB | 60.16 kB | 59.82 kB | 19.49 kB | 19.33 kB | moment.js

287.63 kB | 90.61 kB | 90.07 kB | 32.19 kB | 31.95 kB | jquery.js
287.63 kB | 90.59 kB | 90.07 kB | 32.19 kB | 31.95 kB | jquery.js

342.15 kB | 118.76 kB | 118.14 kB | 44.54 kB | 44.37 kB | vue.js
342.15 kB | 118.61 kB | 118.14 kB | 44.54 kB | 44.37 kB | vue.js

544.10 kB | 72.04 kB | 72.48 kB | 26.18 kB | 26.20 kB | lodash.js

555.77 kB | 273.90 kB | 270.13 kB | 91.19 kB | 90.80 kB | d3.js
555.77 kB | 273.89 kB | 270.13 kB | 91.18 kB | 90.80 kB | d3.js

1.01 MB | 461.13 kB | 458.89 kB | 126.91 kB | 126.71 kB | bundle.min.js
1.01 MB | 461.09 kB | 458.89 kB | 126.91 kB | 126.71 kB | bundle.min.js

1.25 MB | 656.81 kB | 646.76 kB | 164.16 kB | 163.73 kB | three.js
1.25 MB | 656.66 kB | 646.76 kB | 164.14 kB | 163.73 kB | three.js

2.14 MB | 735.33 kB | 724.14 kB | 180.99 kB | 181.07 kB | victory.js
2.14 MB | 735.28 kB | 724.14 kB | 180.98 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 332.27 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 332.23 kB | 331.56 kB | echarts.js

6.69 MB | 2.36 MB | 2.31 MB | 495.04 kB | 488.28 kB | antd.js
6.69 MB | 2.36 MB | 2.31 MB | 494.93 kB | 488.28 kB | antd.js

10.95 MB | 3.51 MB | 3.49 MB | 910.93 kB | 915.50 kB | typescript.js
10.95 MB | 3.51 MB | 3.49 MB | 910.92 kB | 915.50 kB | typescript.js

0 comments on commit f3a36e1

Please sign in to comment.