diff --git a/crates/oxc_minifier/src/peephole/fold_constants.rs b/crates/oxc_minifier/src/peephole/fold_constants.rs index e4d743fad5d19..596bd81855f8d 100644 --- a/crates/oxc_minifier/src/peephole/fold_constants.rs +++ b/crates/oxc_minifier/src/peephole/fold_constants.rs @@ -336,6 +336,12 @@ impl<'a> PeepholeOptimizations { || *right == 0.0 || right.is_nan() || right.is_infinite() + // Small number multiplication. + || (e.operator == BinaryOperator::Multiplication + && left.abs() <= 255.0 + && left.fract() == 0.0 + && right.abs() <= 255.0 + && right.fract() == 0.0) }) .and_then(|_| ctx.eval_binary(e)), BinaryOperator::Division => Self::extract_numeric_values(e) @@ -1751,6 +1757,12 @@ mod test { // test("x = (null + 1) * 2", "x = 2"); // test("x = y + (z * 24 * 60 * 60 * 1000)", "x = y + z * 864E5"); fold("x = y + (z & 24 & 60 & 60 & 1000)", "x = y + (z & 8)"); + fold("x = -1 * -1", "x = 1"); + fold("x = 1 * -1", "x = -1"); + fold("x = 255 * 255", "x = 65025"); + fold("x = -255 * 255", "x = -65025"); + fold("x = -255 * -255", "x = 65025"); + fold_same("x = 256 * 255"); } #[test] diff --git a/crates/oxc_minifier/tests/peephole/esbuild.rs b/crates/oxc_minifier/tests/peephole/esbuild.rs index 60e60a6c44ed4..adc59873fd9b9 100644 --- a/crates/oxc_minifier/tests/peephole/esbuild.rs +++ b/crates/oxc_minifier/tests/peephole/esbuild.rs @@ -901,7 +901,7 @@ fn constant_evaluation_test() { test("x = +{valueOf:()=>1}", "x = +{ valueOf: () => 1 };"); test("x = 3 + 6", "x = 9;"); test("x = 3 - 6", "x = -3;"); - test("x = 3 * 6", "x = 3 * 6;"); + test("x = 3 * 6", "x = 18;"); test("x = 3 / 6", "x = 3 / 6;"); test("x = 3 % 6", "x = 3 % 6;"); test("x = 3 ** 6", "x = 3 ** 6;"); diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 710c471b876e4..d8addf6cff416 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -3,7 +3,7 @@ Original | minified | minified | gzip | gzip | Fixture ------------------------------------------------------------------------------------- 72.14 kB | 23.49 kB | 23.70 kB | 8.47 kB | 8.54 kB | react.development.js -173.90 kB | 59.52 kB | 59.82 kB | 19.18 kB | 19.33 kB | moment.js +173.90 kB | 59.51 kB | 59.82 kB | 19.18 kB | 19.33 kB | moment.js 287.63 kB | 89.33 kB | 90.07 kB | 30.95 kB | 31.95 kB | jquery.js @@ -15,7 +15,7 @@ Original | minified | minified | gzip | gzip | Fixture 1.01 MB | 440.17 kB | 458.89 kB | 122.37 kB | 126.71 kB | bundle.min.js -1.25 MB | 647.00 kB | 646.76 kB | 160.28 kB | 163.73 kB | three.js +1.25 MB | 647 kB | 646.76 kB | 160.28 kB | 163.73 kB | three.js 2.14 MB | 716.13 kB | 724.14 kB | 161.80 kB | 181.07 kB | victory.js