diff --git a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs index 8400d8aa4b6f4..34d625beb18ab 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs @@ -140,6 +140,9 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax { self.try_compress_normal_assignment_to_combined_assignment(e, ctx); self.try_compress_normal_assignment_to_combined_logical_assignment(e, ctx); } + Expression::NewExpression(e) => { + self.try_compress_typed_array_constructor(e, ctx); + } _ => {} } @@ -1038,6 +1041,46 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax { ) } + /// `new Int8Array(0)` -> `new Int8Array()` (also for other TypedArrays) + fn try_compress_typed_array_constructor( + &mut self, + e: &mut NewExpression<'a>, + ctx: Ctx<'a, 'b>, + ) { + let Expression::Identifier(ident) = &e.callee else { return }; + let name = ident.name.as_str(); + if !Self::is_typed_array_name(name) || !ctx.is_global_reference(ident) { + return; + } + + if e.arguments.len() == 1 + && e.arguments[0].as_expression().is_some_and(Expression::is_number_0) + { + e.arguments.clear(); + self.changed = true; + } + } + + /// Whether the name matches any TypedArray name. + /// + /// See for the list of TypedArrays. + fn is_typed_array_name(name: &str) -> bool { + matches!( + name, + "Int8Array" + | "Uint8Array" + | "Uint8ClampedArray" + | "Int16Array" + | "Uint16Array" + | "Int32Array" + | "Uint32Array" + | "Float32Array" + | "Float64Array" + | "BigInt64Array" + | "BigUint64Array" + ) + } + /// `typeof foo === 'number'` -> `typeof foo == 'number'` fn try_compress_type_of_equal_string(&mut self, e: &mut BinaryExpression<'a>) { let op = match e.operator { @@ -1462,6 +1505,26 @@ mod test { test_same("new RegExp(/foo/)"); } + #[test] + fn test_compress_typed_array_constructor() { + test("new Int8Array(0)", "new Int8Array()"); + test("new Uint8Array(0)", "new Uint8Array()"); + test("new Uint8ClampedArray(0)", "new Uint8ClampedArray()"); + test("new Int16Array(0)", "new Int16Array()"); + test("new Uint16Array(0)", "new Uint16Array()"); + test("new Int32Array(0)", "new Int32Array()"); + test("new Uint32Array(0)", "new Uint32Array()"); + test("new Float32Array(0)", "new Float32Array()"); + test("new Float64Array(0)", "new Float64Array()"); + test("new BigInt64Array(0)", "new BigInt64Array()"); + test("new BigUint64Array(0)", "new BigUint64Array()"); + + test_same("var Int8Array; new Int8Array(0)"); + test_same("new Int8Array(1)"); + test_same("new Int8Array(a)"); + test_same("new Int8Array(0, a)"); + } + #[test] #[ignore] fn test_split_comma_expressions() { diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 843ed1dbec8f4..9673b460fb575 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -11,7 +11,7 @@ Original | minified | minified | gzip | gzip | Fixture 544.10 kB | 71.76 kB | 72.48 kB | 26.15 kB | 26.20 kB | lodash.js -555.77 kB | 273.16 kB | 270.13 kB | 90.92 kB | 90.80 kB | d3.js +555.77 kB | 273.15 kB | 270.13 kB | 90.92 kB | 90.80 kB | d3.js 1.01 MB | 460.18 kB | 458.89 kB | 126.77 kB | 126.71 kB | bundle.min.js