mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
feat(minifier): compress new Array(2) -> [,,] (#8344)
For an integer value `n` smaller than 6, `Array(n)` can be compressed to `[,,]` (the number of `,` is `n`).
This commit is contained in:
parent
819c47536c
commit
a69d15f299
2 changed files with 22 additions and 3 deletions
|
|
@ -762,7 +762,24 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
|
|||
Some(ctx.ast.expression_array(span, ctx.ast.vec(), None))
|
||||
}
|
||||
// `new Array(8)` -> `Array(8)`
|
||||
else if let Expression::NumericLiteral(_) = arg {
|
||||
else if let Expression::NumericLiteral(n) = arg {
|
||||
// new Array(2) -> `[,,]`
|
||||
// this does not work with IE8 and below
|
||||
// learned from https://github.com/babel/minify/pull/45
|
||||
#[expect(clippy::cast_possible_truncation)]
|
||||
if n.value.fract() == 0.0 {
|
||||
let n_int = n.value as i64;
|
||||
if (1..=6).contains(&n_int) {
|
||||
return Some(ctx.ast.expression_array(
|
||||
span,
|
||||
ctx.ast.vec_from_iter((0..n_int).map(|_| {
|
||||
ArrayExpressionElement::Elision(Elision { span: SPAN })
|
||||
})),
|
||||
None,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let callee = ctx.ast.expression_identifier_reference(SPAN, "Array");
|
||||
let args = ctx.ast.move_vec(args);
|
||||
Some(ctx.ast.expression_call(span, callee, NONE, args, false))
|
||||
|
|
@ -1143,6 +1160,8 @@ mod test {
|
|||
// One argument
|
||||
test("x = new Array(0)", "x = []");
|
||||
test("x = new Array(\"a\")", "x = [\"a\"]");
|
||||
test("x = new Array(1)", "x = [,]");
|
||||
test("x = new Array(6)", "x = [,,,,,,]");
|
||||
test("x = new Array(7)", "x = Array(7)");
|
||||
test("x = new Array(7n)", "x = [7n]");
|
||||
test("x = new Array(y)", "x = Array(y)");
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ Original | minified | minified | gzip | gzip | Fixture
|
|||
|
||||
544.10 kB | 71.75 kB | 72.48 kB | 26.16 kB | 26.20 kB | lodash.js
|
||||
|
||||
555.77 kB | 272.91 kB | 270.13 kB | 90.92 kB | 90.80 kB | d3.js
|
||||
555.77 kB | 272.82 kB | 270.13 kB | 90.92 kB | 90.80 kB | d3.js
|
||||
|
||||
1.01 MB | 460.22 kB | 458.89 kB | 126.83 kB | 126.71 kB | bundle.min.js
|
||||
|
||||
|
|
@ -23,5 +23,5 @@ Original | minified | minified | gzip | gzip | Fixture
|
|||
|
||||
6.69 MB | 2.32 MB | 2.31 MB | 492.72 kB | 488.28 kB | antd.js
|
||||
|
||||
10.95 MB | 3.50 MB | 3.49 MB | 908.29 kB | 915.50 kB | typescript.js
|
||||
10.95 MB | 3.50 MB | 3.49 MB | 908.28 kB | 915.50 kB | typescript.js
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue