diff --git a/Cargo.lock b/Cargo.lock index 292881f9c556..f55129579cbd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4952,6 +4952,8 @@ name = "swc_fast_ts_strip" version = "0.1.7" dependencies = [ "anyhow", + "codspeed-criterion-compat", + "criterion", "serde", "swc_common", "swc_ecma_ast", diff --git a/crates/swc_fast_ts_strip/Cargo.toml b/crates/swc_fast_ts_strip/Cargo.toml index 3c40a092b856..e9e89ace22d1 100644 --- a/crates/swc_fast_ts_strip/Cargo.toml +++ b/crates/swc_fast_ts_strip/Cargo.toml @@ -20,4 +20,10 @@ swc_ecma_parser = { version = "0.146.9", path = "../swc_ecma_parser" } swc_ecma_visit = { version = "0.101.0", path = "../swc_ecma_visit" } [dev-dependencies] -testing = { version = "0.36.0", path = "../testing" } +codspeed-criterion-compat = { workspace = true } +criterion = { workspace = true } +testing = { version = "0.36.0", path = "../testing" } + +[[bench]] +harness = false +name = "assets" diff --git a/crates/swc_fast_ts_strip/benches/assets.rs b/crates/swc_fast_ts_strip/benches/assets.rs new file mode 100644 index 000000000000..5cb88c9d1a4a --- /dev/null +++ b/crates/swc_fast_ts_strip/benches/assets.rs @@ -0,0 +1,31 @@ +use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion}; +use swc_fast_ts_strip::{operate, Options}; + +static SOURCE: &str = include_str!("assets/test.ts"); + +fn fast_ts(c: &mut Criterion) { + c.bench_function("typescript/fast-strip", fast_typescript); +} +fn fast_typescript(b: &mut Bencher) { + b.iter(|| { + ::testing::run_test(false, |cm, handler| { + black_box(operate( + &cm, + handler, + black_box(SOURCE.to_string()), + Options { + module: None, + filename: None, + parser: Default::default(), + }, + )) + .unwrap(); + + Ok(()) + }) + .unwrap(); + }); +} + +criterion_group!(benches, fast_ts); +criterion_main!(benches); diff --git a/crates/swc_fast_ts_strip/benches/assets/test.ts b/crates/swc_fast_ts_strip/benches/assets/test.ts new file mode 100644 index 000000000000..849df1cddf31 --- /dev/null +++ b/crates/swc_fast_ts_strip/benches/assets/test.ts @@ -0,0 +1,198 @@ +let x /**/: number/**/ = 1!; +// ^^^^^^^^ ^ + +[] as [] satisfies []; +// ^^^^^^^^^^^^^^^^^^ + +("test"); +//^^^^^^^^ + +class C /**//*︎*/ extends Array/**/ /*︎*/ implements I, J/*︎*/ { + // ^^^^^ ^^^ ^^^^^^^^^^^^^^ + readonly field/**/: string/**/ = ""; + // ^^^^^^^^ ^^^^^^^^ + static accessor f1; + private f2/**/!/**/: string/*︎*/; + // ^^^^^^^ ^ ^^^^^^^^ + declare f3: any; + // ^^^^^^^^^^^^^^^^ declared property + + public method/**//*︎*/(/*︎*/this: T,/**/ a? /*︎*/: string/**/)/*︎*/: void/*︎*/ { + // ^^^^^^ ^^^ ^^^^^^^^ ^ ^^^^^^^^ ^^^^^^ + } + + [key: string]: any; + // ^^^^^^^^^^^^^^^^^^^ index signature + + get g(): any { return 1 }; + // ^^^^^ + set g(v: any) { }; + // ^^^^^ +} + +class D extends C { + // ^^^^^ + override method(...args): any { } + // ^^^^^^^^ ^^^^^ +} + +abstract class A { + // ^^^^^^^^ + abstract a; + // ^^^^^^^^^^^ abstract property + b; + abstract method(); + // ^^^^^^^^^^^^^^^^^^ abstract method +} + +{ + let m = new (Map!)([]!); + // ^ ^^^^^^^^^^^^^^^^ ^ +} + +{ + let a = (foo!); + // ^ ^^^^^ +} + +{ + let a = (foo!)([]!); + // ^ ^^^^^ ^ +} + +{ + let f = function (p: any) { } + // ^^^^^ +} + +{ + function overload(): number; + // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overload + function overload(): any { } + // ^^^^^ +} + +/** @doc */ +interface I { } +// ^^^^^^^^^^^ interface + +void 0; + +/** @doc */ +type J = I; +// ^^^^^^^^ type alias + +/**/import type T from "node:assert"; +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `import type` + +/**/export type { I }; +// ^^^^^^^^^^^^^^^^^^ `export type` + +/**/export type * from "node:buffer"; +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `export type *` + +import { type AssertPredicate/**/, deepEqual } from "node:assert"; +// ^^^^^^^^^^^^^^^^^^^^^^^^^ + +export { + C, + type T, + // ^^^^^^ +} + +/**/export type T2 = 1; +// ^^^^^^^^^^^^^^^^^^^ + +function foo(p: any = (): any => 1): any { + // ^^^ ^^^^^ ^^^^^ ^^^^^ + return p as any; + // ^^^^^^ +} + +/**/declare enum E1 { } +// ^^^^^^^^^^^^^^^^^^ `declare enum` + +void 0; + +/**/declare namespace N { } +// ^^^^^^^^^^^^^^^^^^^^^^ `declare namespace` + +void 0; + +/**/declare module M { } +// ^^^^^^^^^^^^^^^^^^^ `declare module` + +void 0; + +/**/declare let a; +// ^^^^^^^^^^^^^^ `declare let` + +void 0; + +/**/declare class DeclaredClass { } +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `declare class` + +void 0; + +/**/declare function DeclaredFunction(): void; +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `declare function` + +void 0; + +// `=>` spanning line cases: +{ + () + : any => + 1 +}; +{ + (): + any => + 1 +}; +{ + ( + ) + : any => + 1 +}; +{ + ( + ): ( + | any + ) => + 1 +}; +{ + ( + ): + NonNullable => + 1 +}; +{ + (a, b, c: D = [] as any/*comment-1*/)/*comment-2*/: + any => + 1 +}; + + +(): + any => + 1; + +{ + (a, b, c: D = [] as any/*comment-1*/)/*comment-2*/: + /*comment-3*/any/*comment-4*/ => + 1 +}; + +type 任意の型 = any; + +(): + 任意の型 => + 1; + +()/*comment-1*/:/*comment-2*/ +/*comment-3*/任意の型/*comment-4*/ => + 1; \ No newline at end of file