Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,21 @@ mod test {
if (!x) {
for (;;) foo();
for (;;) bar();
} else if (y) for (;;) f();",
} else for (;;) f();",
"
if (x) {
if (y) for (; ; ) f();
} else {
for (; ; ) foo();
for (; ; ) bar();}",
if (x) for (;;) f();
else {
for (;;) foo();
for (;;) bar();
}
",
);
test_same(
"
if (!x) {
for (;;) foo();
for (;;) bar();
} else if (y) for (;;) f();",
);
test_same("if(!a&&!b) {for(;;)foo(); for(;;)bar()} else if(y) for(;;) f()");
}
Expand Down
26 changes: 19 additions & 7 deletions crates/oxc_minifier/src/peephole/minimize_if_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ impl<'a> PeepholeOptimizations {
// "yes" is not missing (and is not an expression)
if let Some(alternate) = &mut if_stmt.alternate {
// "yes" is not missing (and is not an expression) and "no" is not missing
if let Expression::UnaryExpression(unary_expr) = &mut if_stmt.test
if !matches!(alternate, Statement::IfStatement(_))
&& let Expression::UnaryExpression(unary_expr) = &mut if_stmt.test
&& unary_expr.operator.is_not()
{
// "if (!a) return b; else return c;" => "if (a) return c; else return b;"
Expand All @@ -98,6 +99,7 @@ impl<'a> PeepholeOptimizations {
Self::wrap_to_avoid_ambiguous_else(if_stmt, ctx);
ctx.state.changed = true;
}
// "if (!a) {} else if (b) {}" => "if (!a) {} if (b) {}" is handled by minimize_statements
// "if (a) return b; else {}" => "if (a) return b;" is handled by remove_dead_code
} else {
// "no" is missing
Expand Down Expand Up @@ -169,6 +171,19 @@ mod test {
}",
);

test(
"function bar() {
if (!x) {
return null;
} else {
return foo;
}
}",
"function bar() {
return x ? foo : null;
}",
);

test(
"function bar() {
if (!x) {
Expand All @@ -180,12 +195,9 @@ mod test {
}
}",
"function bar() {
if (x) {
if (y)
return foo;
if (z)
return bar;
} else return null;
if (!x) return null;
if (y) return foo;
if (z) return bar;
}",
);

Expand Down
8 changes: 4 additions & 4 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ Original | minified | minified | gzip | gzip | Iterations | Fi

555.77 kB | 267.39 kB | 270.13 kB | 88.00 kB | 90.80 kB | 2 | d3.js

1.01 MB | 439.38 kB | 458.89 kB | 122.06 kB | 126.71 kB | 2 | bundle.min.js
1.01 MB | 439.37 kB | 458.89 kB | 122.06 kB | 126.71 kB | 2 | bundle.min.js

1.25 MB | 642.66 kB | 646.76 kB | 159.40 kB | 163.73 kB | 2 | three.js

2.14 MB | 711.14 kB | 724.14 kB | 160.43 kB | 181.07 kB | 2 | victory.js
2.14 MB | 711.11 kB | 724.14 kB | 160.43 kB | 181.07 kB | 2 | victory.js

3.20 MB | 1.00 MB | 1.01 MB | 322.59 kB | 331.56 kB | 3 | echarts.js
3.20 MB | 1.00 MB | 1.01 MB | 322.58 kB | 331.56 kB | 3 | echarts.js

6.69 MB | 2.22 MB | 2.31 MB | 458.44 kB | 488.28 kB | 4 | antd.js

10.95 MB | 3.33 MB | 3.49 MB | 853.35 kB | 915.50 kB | 4 | typescript.js
10.95 MB | 3.33 MB | 3.49 MB | 853.30 kB | 915.50 kB | 4 | typescript.js

6 changes: 3 additions & 3 deletions tasks/track_memory_allocations/allocs_minifier.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
File | File size || Sys allocs | Sys reallocs || Arena allocs | Arena reallocs | Arena bytes
-------------------------------------------------------------------------------------------------------------------------------------------
checker.ts | 2.92 MB || 4128 | 1669 || 152592 | 28237
checker.ts | 2.92 MB || 4128 | 1669 || 152600 | 28244

cal.com.tsx | 1.06 MB || 21098 | 474 || 37138 | 4586

RadixUIAdoptionSection.jsx | 2.52 kB || 65 | 3 || 30 | 6

pdf.mjs | 567.30 kB || 4691 | 569 || 47453 | 7730
pdf.mjs | 567.30 kB || 4691 | 569 || 47464 | 7730

antd.js | 6.69 MB || 10723 | 2505 || 331649 | 69358
antd.js | 6.69 MB || 10723 | 2505 || 331648 | 69358

binder.ts | 193.08 kB || 431 | 119 || 7075 | 824

Loading