Skip to content

Commit

Permalink
feat(es/minifier): Evaluate more toFixed expressions (#8109)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored Oct 13, 2023
1 parent 787a465 commit 7da3f52
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 110 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ radix_fmt = "=1.0.0"
rayon = { version = "1.7.0", optional = true }
regex = "1.5.3"
rustc-hash = "1.1.0"
ryu-js = "0.2.2"
ryu-js = "1.0.0"
serde = { version = "1.0.118", features = ["derive"] }
serde_json = "1.0.61"
tracing = "0.1.37"
Expand Down
31 changes: 3 additions & 28 deletions crates/swc_ecma_minifier/src/compress/pure/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ impl Pure<'_> {
// 3. Assert: If fractionDigits is undefined, then f is 0.
.map_or(Some(0f64), |arg| eval_as_number(&self.expr_ctx, &arg.expr))
{
let f = precision.trunc() as usize;
let f = precision.trunc() as u8;

// 4. If f is not finite, throw a RangeError exception.
// 5. If f < 0 or f > 100, throw a RangeError exception.
Expand All @@ -341,33 +341,8 @@ impl Pure<'_> {
return;
}

// 6. If x is not finite, return Number::toString(x, 10).

let x = {
let x = num.value;
// 7. Set x to ℝ(x).
if x == -0. {
0.
} else {
x
}
};
// 8. Let s be the empty String.
// 9. If x < 0, then a. Set s to "-". b. Set x to -x.
// 10. If x ≥ 10**21, then a. Let m be ! ToString(𝔽(x)).
let value = if x >= 1e21 || x <= -1e21 {
format!("{:e}", x).replace('e', "e+")
} else {
// 11. Else,

if x.fract() != 0. || f != 0 {
// TODO: rust built-in format cannot handle ecma262 `1.25.toFixed(1)`

return;
} else {
format!("{:.*}", f, x)
}
};
let mut buffer = ryu_js::Buffer::new();
let value = buffer.format_to_fixed(num.value, f);

self.changed = true;
report_change!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6018,7 +6018,7 @@
$({
target: "Number",
proto: !0,
forced: nativeToFixed && ("0.000" !== 0.00008.toFixed(3) || "1" !== 0.9.toFixed(0) || "1.25" !== 1.255.toFixed(2)) || !fails(function() {
forced: !fails(function() {
nativeToFixed.call({});
})
}, {
Expand Down
32 changes: 16 additions & 16 deletions crates/swc_ecma_minifier/tests/fixture/issues/6957/1/output.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
export function foo() {
alert(1..toFixed(1));
alert("1.0");
alert("0");
alert(0..toFixed(1));
alert(0..toFixed(2));
alert(0..toFixed(3));
alert(10..toFixed(1));
alert(20..toFixed(2));
alert(30..toFixed(3));
alert(100..toFixed(1));
alert(100..toFixed(2));
alert(100..toFixed(3));
alert(110..toFixed(1));
alert(110..toFixed(2));
alert(110..toFixed(3));
alert(110..toFixed(4));
alert(1110..toFixed(4));
alert(11110..toFixed(4));
alert("0.0");
alert("0.00");
alert("0.000");
alert("10.0");
alert("20.00");
alert("30.000");
alert("100.0");
alert("100.00");
alert("100.000");
alert("110.0");
alert("110.00");
alert("110.000");
alert("110.0000");
alert("1110.0000");
alert("11110.0000");
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
assertEquals("1", 0.5.toFixed(0), "0.5.toFixed(0)");
assertEquals("-1", (-0.5).toFixed(0), "(-0.5).toFixed(0)");
assertEquals("1", "1", "0.5.toFixed(0)");
assertEquals("-1", "-1", "(-0.5).toFixed(0)");
Original file line number Diff line number Diff line change
Expand Up @@ -120,74 +120,74 @@ assertEquals("NaN", NaN.toFixed(2));
assertEquals("Infinity", (1 / 0).toFixed(2));
assertEquals("-Infinity", (-1 / 0).toFixed(2));
assertEquals("1.1111111111111111e+21", "1.1111111111111111e+21");
assertEquals("0.1", 0.1.toFixed(1));
assertEquals("0.10", 0.1.toFixed(2));
assertEquals("0.100", 0.1.toFixed(3));
assertEquals("0.01", 0.01.toFixed(2));
assertEquals("0.010", 0.01.toFixed(3));
assertEquals("0.0100", 0.01.toFixed(4));
assertEquals("0.00", 0.001.toFixed(2));
assertEquals("0.001", 0.001.toFixed(3));
assertEquals("0.0010", 0.001.toFixed(4));
assertEquals("1.0000", 1..toFixed(4));
assertEquals("1.0", 1..toFixed(1));
assertEquals("0.1", "0.1");
assertEquals("0.10", "0.10");
assertEquals("0.100", "0.100");
assertEquals("0.01", "0.01");
assertEquals("0.010", "0.010");
assertEquals("0.0100", "0.0100");
assertEquals("0.00", "0.00");
assertEquals("0.001", "0.001");
assertEquals("0.0010", "0.0010");
assertEquals("1.0000", "1.0000");
assertEquals("1.0", "1.0");
assertEquals("1", "1");
assertEquals("12", "12");
assertEquals("1", "1");
assertEquals("12", "12");
assertEquals("1", "1");
assertEquals("12", "12");
assertEquals("1", 1.1.toFixed(0));
assertEquals("12", 12.1.toFixed(0));
assertEquals("1", 1.12.toFixed(0));
assertEquals("12", 12.12.toFixed(0));
assertEquals("0.0000006", 0.0000006.toFixed(7));
assertEquals("0.00000006", 0.00000006.toFixed(8));
assertEquals("0.000000060", 0.00000006.toFixed(9));
assertEquals("0.0000000600", 0.00000006.toFixed(10));
assertEquals("0.0000006", "0.0000006");
assertEquals("0.00000006", "0.00000006");
assertEquals("0.000000060", "0.000000060");
assertEquals("0.0000000600", "0.0000000600");
assertEquals("0", "0");
assertEquals("0.0", 0..toFixed(1));
assertEquals("0.00", 0..toFixed(2));
assertEquals("0.0", "0.0");
assertEquals("0.00", "0.00");
assertEquals("-1.1111111111111111e+21", "-1.1111111111111111e+21");
assertEquals("-0.1", (-0.1).toFixed(1));
assertEquals("-0.10", (-0.1).toFixed(2));
assertEquals("-0.100", (-0.1).toFixed(3));
assertEquals("-0.01", (-0.01).toFixed(2));
assertEquals("-0.010", (-0.01).toFixed(3));
assertEquals("-0.0100", (-0.01).toFixed(4));
assertEquals("-0.00", (-0.001).toFixed(2));
assertEquals("-0.001", (-0.001).toFixed(3));
assertEquals("-0.0010", (-0.001).toFixed(4));
assertEquals("-1.0000", (-1).toFixed(4));
assertEquals("-1.0", (-1).toFixed(1));
assertEquals("-0.1", "-0.1");
assertEquals("-0.10", "-0.10");
assertEquals("-0.100", "-0.100");
assertEquals("-0.01", "-0.01");
assertEquals("-0.010", "-0.010");
assertEquals("-0.0100", "-0.0100");
assertEquals("-0.00", "-0.00");
assertEquals("-0.001", "-0.001");
assertEquals("-0.0010", "-0.0010");
assertEquals("-1.0000", "-1.0000");
assertEquals("-1.0", "-1.0");
assertEquals("-1", "-1");
assertEquals("-1", "-1");
assertEquals("-1", (-1.1).toFixed(0));
assertEquals("-12", (-12.1).toFixed(0));
assertEquals("-1", (-1.12).toFixed(0));
assertEquals("-12", (-12.12).toFixed(0));
assertEquals("-0.0000006", (-0.0000006).toFixed(7));
assertEquals("-0.00000006", (-0.00000006).toFixed(8));
assertEquals("-0.000000060", (-0.00000006).toFixed(9));
assertEquals("-0.0000000600", (-0.00000006).toFixed(10));
assertEquals("-12", "-12");
assertEquals("-1", "-1");
assertEquals("-12", "-12");
assertEquals("-0.0000006", "-0.0000006");
assertEquals("-0.00000006", "-0.00000006");
assertEquals("-0.000000060", "-0.000000060");
assertEquals("-0.0000000600", "-0.0000000600");
assertEquals("0", "0");
assertEquals("0.0", (-0).toFixed(1));
assertEquals("0.00", (-0).toFixed(2));
assertEquals("0.0", "0.0");
assertEquals("0.00", "0.00");
assertEquals("1000", "1000");
assertEquals("0", 0.00001.toFixed());
assertEquals("0.00001", 0.00001.toFixed(5));
assertEquals("0.00000000000000000010", 0.0000000000000000001.toFixed(20));
assertEquals("0.00001000000000000", 0.00001.toFixed(17));
assertEquals("1.00000000000000000", 1..toFixed(17));
assertEquals("0", "0");
assertEquals("0.00001", "0.00001");
assertEquals("0.00000000000000000010", "0.00000000000000000010");
assertEquals("0.00001000000000000", "0.00001000000000000");
assertEquals("1.00000000000000000", "1.00000000000000000");
assertEquals("1000000000000000128", "1000000000000000128");
assertEquals("100000000000000128.0", 100000000000000128..toFixed(1));
assertEquals("10000000000000128.00", 10000000000000128..toFixed(2));
assertEquals("10000000000000128.00000000000000000000", 10000000000000128..toFixed(20));
assertEquals("100000000000000128.0", "100000000000000128.0");
assertEquals("10000000000000128.00", "10000000000000128.00");
assertEquals("10000000000000128.00000000000000000000", "10000000000000128.00000000000000000000");
assertEquals("0", "0");
assertEquals("-42.000", (-42).toFixed(3));
assertEquals("-42.000", "-42.000");
assertEquals("-1000000000000000128", "-1000000000000000128");
assertEquals("-0.00000000000000000010", (-0.0000000000000000001).toFixed(20));
assertEquals("0.12312312312312299889", 0.123123123123123.toFixed(20));
assertEquals("1", 0.5.toFixed(0), "0.5.toFixed(0)");
assertEquals("-1", (-0.5).toFixed(0), "(-0.5).toFixed(0)");
assertEquals("1.3", 1.25.toFixed(1), "1.25.toFixed(1)");
assertEquals("234.2040", 234.20405.toFixed(4), "234.2040.toFixed(4)");
assertEquals("234.2041", 234.2040506.toFixed(4));
assertEquals("-0.00000000000000000010", "-0.00000000000000000010");
assertEquals("0.12312312312312299889", "0.12312312312312299889");
assertEquals("1", "1", "0.5.toFixed(0)");
assertEquals("-1", "-1", "(-0.5).toFixed(0)");
assertEquals("1.3", "1.3", "1.25.toFixed(1)");
assertEquals("234.2040", "234.2040", "234.2040.toFixed(4)");
assertEquals("234.2041", "234.2041");
assertEquals("1e+0", "1e+0");
assertEquals("1.1e+1", "1.1e+1");
assertEquals("1.12e+2", "1.12e+2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ console.log("A");
console.log(7);
console.log(0.32999315767856785);
console.log(1.2543732512566947);
console.log(1.6093984514472044.toFixed(15));
console.log("1.609398451447204");
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_typescript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bench = false
[dependencies]
serde = { version = "1.0.118", features = ["derive"] }

ryu-js = "0.2.2"
ryu-js = "1.0.0"
swc_atoms = { version = "0.6.0", path = "../swc_atoms" }
swc_common = { version = "0.33.0", path = "../swc_common" }
swc_ecma_ast = { version = "0.110.0", path = "../swc_ecma_ast" }
Expand Down

1 comment on commit 7da3f52

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7da3f52 Previous: affb6fb Ratio
es/full/bugs-1 290281 ns/iter (± 3424) 283618 ns/iter (± 4469) 1.02
es/full/minify/libraries/antd 1416638674 ns/iter (± 21486186) 1422322416 ns/iter (± 8739396) 1.00
es/full/minify/libraries/d3 297774286 ns/iter (± 3440942) 297412872 ns/iter (± 5038966) 1.00
es/full/minify/libraries/echarts 1134630164 ns/iter (± 8866371) 1128557526 ns/iter (± 14480562) 1.01
es/full/minify/libraries/jquery 89532801 ns/iter (± 309873) 88912879 ns/iter (± 190946) 1.01
es/full/minify/libraries/lodash 104753468 ns/iter (± 301989) 103942791 ns/iter (± 218466) 1.01
es/full/minify/libraries/moment 52794327 ns/iter (± 160922) 52264597 ns/iter (± 121762) 1.01
es/full/minify/libraries/react 19047481 ns/iter (± 158342) 18838198 ns/iter (± 63071) 1.01
es/full/minify/libraries/terser 232872722 ns/iter (± 1824743) 232281461 ns/iter (± 1723136) 1.00
es/full/minify/libraries/three 413114866 ns/iter (± 1337935) 415451473 ns/iter (± 2192387) 0.99
es/full/minify/libraries/typescript 2846530066 ns/iter (± 9904541) 2842484338 ns/iter (± 16792163) 1.00
es/full/minify/libraries/victory 609073673 ns/iter (± 3471456) 620651643 ns/iter (± 4743234) 0.98
es/full/minify/libraries/vue 127489727 ns/iter (± 313271) 127009310 ns/iter (± 583293) 1.00
es/full/codegen/es3 33947 ns/iter (± 97) 33808 ns/iter (± 126) 1.00
es/full/codegen/es5 34033 ns/iter (± 90) 33926 ns/iter (± 90) 1.00
es/full/codegen/es2015 34134 ns/iter (± 86) 33808 ns/iter (± 119) 1.01
es/full/codegen/es2016 34034 ns/iter (± 64) 33799 ns/iter (± 142) 1.01
es/full/codegen/es2017 34212 ns/iter (± 108) 33879 ns/iter (± 141) 1.01
es/full/codegen/es2018 34008 ns/iter (± 79) 33838 ns/iter (± 106) 1.01
es/full/codegen/es2019 34039 ns/iter (± 155) 33667 ns/iter (± 73) 1.01
es/full/codegen/es2020 34075 ns/iter (± 108) 33691 ns/iter (± 67) 1.01
es/full/all/es3 174893038 ns/iter (± 919968) 176280263 ns/iter (± 1450696) 0.99
es/full/all/es5 168172736 ns/iter (± 778216) 170809229 ns/iter (± 1506822) 0.98
es/full/all/es2015 127766975 ns/iter (± 614183) 127920746 ns/iter (± 1041117) 1.00
es/full/all/es2016 126209740 ns/iter (± 1007644) 126359371 ns/iter (± 1198604) 1.00
es/full/all/es2017 125854278 ns/iter (± 752301) 126608421 ns/iter (± 1064652) 0.99
es/full/all/es2018 123818878 ns/iter (± 568628) 123814405 ns/iter (± 786046) 1.00
es/full/all/es2019 123363559 ns/iter (± 564297) 123835317 ns/iter (± 1312301) 1.00
es/full/all/es2020 119385734 ns/iter (± 757408) 119921061 ns/iter (± 844210) 1.00
es/full/parser 561227 ns/iter (± 4914) 558607 ns/iter (± 10321) 1.00
es/full/base/fixer 18440 ns/iter (± 194) 17181 ns/iter (± 161) 1.07
es/full/base/resolver_and_hygiene 84587 ns/iter (± 346) 83673 ns/iter (± 269) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.