Skip to content

Commit

Permalink
feat(minifier): minify call expressionsto Number
Browse files Browse the repository at this point in the history
  • Loading branch information
camc314 committed Jan 5, 2025
1 parent cece5da commit d09bc1e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
4 changes: 4 additions & 0 deletions crates/oxc_ecmascript/src/to_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl<'a> ToNumber<'a> for Expression<'a> {
use crate::StringToNumber;
Some(lit.value.as_str().string_to_number())
}
Expression::UnaryExpression(unary) if unary.operator.is_not() => {
let number = unary.argument.to_number()?;
Some(if number == 0.0 { 1.0 } else { 0.0 })
}
_ => None,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use oxc_allocator::Vec;
use oxc_ast::{ast::*, NONE};
use oxc_ecmascript::{constant_evaluation::ConstantEvaluation, ToInt32, ToJsString};
use oxc_ecmascript::{constant_evaluation::ConstantEvaluation, ToInt32, ToJsString, ToNumber};
use oxc_semantic::IsGlobalReference;
use oxc_span::{GetSpan, SPAN};
use oxc_syntax::{
Expand Down Expand Up @@ -671,6 +671,19 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
BinaryOperator::Addition,
ctx.ast.move_expression(arg),
))
} else if call_expr.callee.is_global_reference_name("Number", ctx.symbols()) {
let number = call_expr
.arguments
.get_mut(0)
.and_then(|arg| arg.as_expression_mut())?
.to_number()?;

Some(ctx.ast.expression_numeric_literal(
call_expr.span,
number,
None,
NumberBase::Decimal,
))
} else {
None
}
Expand Down Expand Up @@ -1373,4 +1386,12 @@ mod test {
test("f(...[1,,,3])", "f(1, void 0, void 0, 3)");
test("f(a, ...[])", "f(a)");
}

#[test]
fn test_fold_number_call() {
test("Number(0)", "0");
test("Number(true)", "1");
test("Number(false)", "0");
test("Number('foo')", "NaN");
}
}
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.69 kB | 23.70 kB | 8.62 kB | 8.54 kB | react.development.js

173.90 kB | 59.87 kB | 59.82 kB | 19.43 kB | 19.33 kB | moment.js
173.90 kB | 59.93 kB | 59.82 kB | 19.43 kB | 19.33 kB | moment.js

287.63 kB | 90.18 kB | 90.07 kB | 32.10 kB | 31.95 kB | jquery.js
287.63 kB | 90.31 kB | 90.07 kB | 32.11 kB | 31.95 kB | jquery.js

342.15 kB | 118.23 kB | 118.14 kB | 44.52 kB | 44.37 kB | vue.js
342.15 kB | 118.39 kB | 118.14 kB | 44.53 kB | 44.37 kB | vue.js

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

555.77 kB | 273.17 kB | 270.13 kB | 90.96 kB | 90.80 kB | d3.js
555.77 kB | 273.18 kB | 270.13 kB | 90.96 kB | 90.80 kB | d3.js

1.01 MB | 460.34 kB | 458.89 kB | 126.85 kB | 126.71 kB | bundle.min.js
1.01 MB | 460.37 kB | 458.89 kB | 126.86 kB | 126.71 kB | bundle.min.js

1.25 MB | 652.70 kB | 646.76 kB | 163.54 kB | 163.73 kB | three.js
1.25 MB | 652.89 kB | 646.76 kB | 163.57 kB | 163.73 kB | three.js

2.14 MB | 726.18 kB | 724.14 kB | 180.17 kB | 181.07 kB | victory.js
2.14 MB | 726.26 kB | 724.14 kB | 180.19 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 331.88 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 331.90 kB | 331.56 kB | echarts.js

6.69 MB | 2.32 MB | 2.31 MB | 492.84 kB | 488.28 kB | antd.js
6.69 MB | 2.32 MB | 2.31 MB | 492.93 kB | 488.28 kB | antd.js

10.95 MB | 3.50 MB | 3.49 MB | 909.21 kB | 915.50 kB | typescript.js
10.95 MB | 3.50 MB | 3.49 MB | 909.23 kB | 915.50 kB | typescript.js

0 comments on commit d09bc1e

Please sign in to comment.