perf(es/typescript): Add a benchmark for fast TS strip (#9205)

This commit is contained in:
Donny/강동윤 2024-07-11 18:22:31 +09:00 committed by GitHub
parent b0a3987cf5
commit 7d9364cad3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 238 additions and 1 deletions

2
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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);

View File

@ -0,0 +1,198 @@
let x /**/: number/**/ = 1!;
// ^^^^^^^^ ^
[] as [] satisfies [];
// ^^^^^^^^^^^^^^^^^^
(<string>"test");
//^^^^^^^^
class C /**/<T>/**/ extends Array/**/<T> /**/ implements I, J/**/ {
// ^^^^^ ^^^ ^^^^^^^^^^^^^^
readonly field/**/: string/**/ = "";
// ^^^^^^^^ ^^^^^^^^
static accessor f1;
private f2/**/!/**/: string/**/;
// ^^^^^^^ ^ ^^^^^^^^
declare f3: any;
// ^^^^^^^^^^^^^^^^ declared property
public method/**/<T>/**/(/**/this: T,/**/ a? /**/: string/**/)/**/: void/**/ {
// ^^^^^^ ^^^ ^^^^^^^^ ^ ^^^^^^^^ ^^^^^^
}
[key: string]: any;
// ^^^^^^^^^^^^^^^^^^^ index signature
get g(): any { return 1 };
// ^^^^^
set g(v: any) { };
// ^^^^^
}
class D extends C<any> {
// ^^^^^
override method(...args): any { }
// ^^^^^^^^ ^^^^^
}
abstract class A {
// ^^^^^^^^
abstract a;
// ^^^^^^^^^^^ abstract property
b;
abstract method();
// ^^^^^^^^^^^^^^^^^^ abstract method
}
{
let m = new (Map!)<string, number>([]!);
// ^ ^^^^^^^^^^^^^^^^ ^
}
{
let a = (foo!)<any>;
// ^ ^^^^^
}
{
let a = (foo!)<any>([]!);
// ^ ^^^^^ ^
}
{
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<T>(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<any
> =>
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;