Skip to content

Commit 7d9364c

Browse files
authoredJul 11, 2024··
perf(es/typescript): Add a benchmark for fast TS strip (#9205)
1 parent b0a3987 commit 7d9364c

File tree

4 files changed

+238
-1
lines changed

4 files changed

+238
-1
lines changed
 

‎Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/swc_fast_ts_strip/Cargo.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ swc_ecma_parser = { version = "0.146.9", path = "../swc_ecma_parser" }
2020
swc_ecma_visit = { version = "0.101.0", path = "../swc_ecma_visit" }
2121

2222
[dev-dependencies]
23-
testing = { version = "0.36.0", path = "../testing" }
23+
codspeed-criterion-compat = { workspace = true }
24+
criterion = { workspace = true }
25+
testing = { version = "0.36.0", path = "../testing" }
26+
27+
[[bench]]
28+
harness = false
29+
name = "assets"
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Bencher, Criterion};
2+
use swc_fast_ts_strip::{operate, Options};
3+
4+
static SOURCE: &str = include_str!("assets/test.ts");
5+
6+
fn fast_ts(c: &mut Criterion) {
7+
c.bench_function("typescript/fast-strip", fast_typescript);
8+
}
9+
fn fast_typescript(b: &mut Bencher) {
10+
b.iter(|| {
11+
::testing::run_test(false, |cm, handler| {
12+
black_box(operate(
13+
&cm,
14+
handler,
15+
black_box(SOURCE.to_string()),
16+
Options {
17+
module: None,
18+
filename: None,
19+
parser: Default::default(),
20+
},
21+
))
22+
.unwrap();
23+
24+
Ok(())
25+
})
26+
.unwrap();
27+
});
28+
}
29+
30+
criterion_group!(benches, fast_ts);
31+
criterion_main!(benches);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
let x /**/: number/**/ = 1!;
2+
// ^^^^^^^^ ^
3+
4+
[] as [] satisfies [];
5+
// ^^^^^^^^^^^^^^^^^^
6+
7+
(<string>"test");
8+
//^^^^^^^^
9+
10+
class C /**/<T>/*︎*/ extends Array/**/<T> /*︎*/ implements I, J/*︎*/ {
11+
// ^^^^^ ^^^ ^^^^^^^^^^^^^^
12+
readonly field/**/: string/**/ = "";
13+
// ^^^^^^^^ ^^^^^^^^
14+
static accessor f1;
15+
private f2/**/!/**/: string/*︎*/;
16+
// ^^^^^^^ ^ ^^^^^^^^
17+
declare f3: any;
18+
// ^^^^^^^^^^^^^^^^ declared property
19+
20+
public method/**/<T>/*︎*/(/*︎*/this: T,/**/ a? /*︎*/: string/**/)/**/: void/**/ {
21+
// ^^^^^^ ^^^ ^^^^^^^^ ^ ^^^^^^^^ ^^^^^^
22+
}
23+
24+
[key: string]: any;
25+
// ^^^^^^^^^^^^^^^^^^^ index signature
26+
27+
get g(): any { return 1 };
28+
// ^^^^^
29+
set g(v: any) { };
30+
// ^^^^^
31+
}
32+
33+
class D extends C<any> {
34+
// ^^^^^
35+
override method(...args): any { }
36+
// ^^^^^^^^ ^^^^^
37+
}
38+
39+
abstract class A {
40+
// ^^^^^^^^
41+
abstract a;
42+
// ^^^^^^^^^^^ abstract property
43+
b;
44+
abstract method();
45+
// ^^^^^^^^^^^^^^^^^^ abstract method
46+
}
47+
48+
{
49+
let m = new (Map!)<string, number>([]!);
50+
// ^ ^^^^^^^^^^^^^^^^ ^
51+
}
52+
53+
{
54+
let a = (foo!)<any>;
55+
// ^ ^^^^^
56+
}
57+
58+
{
59+
let a = (foo!)<any>([]!);
60+
// ^ ^^^^^ ^
61+
}
62+
63+
{
64+
let f = function (p: any) { }
65+
// ^^^^^
66+
}
67+
68+
{
69+
function overload(): number;
70+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overload
71+
function overload(): any { }
72+
// ^^^^^
73+
}
74+
75+
/** @doc */
76+
interface I { }
77+
// ^^^^^^^^^^^ interface
78+
79+
void 0;
80+
81+
/** @doc */
82+
type J = I;
83+
// ^^^^^^^^ type alias
84+
85+
/**/import type T from "node:assert";
86+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `import type`
87+
88+
/**/export type { I };
89+
// ^^^^^^^^^^^^^^^^^^ `export type`
90+
91+
/**/export type * from "node:buffer";
92+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `export type *`
93+
94+
import { type AssertPredicate/**/, deepEqual } from "node:assert";
95+
// ^^^^^^^^^^^^^^^^^^^^^^^^^
96+
97+
export {
98+
C,
99+
type T,
100+
// ^^^^^^
101+
}
102+
103+
/**/export type T2 = 1;
104+
// ^^^^^^^^^^^^^^^^^^^
105+
106+
function foo<T>(p: any = (): any => 1): any {
107+
// ^^^ ^^^^^ ^^^^^ ^^^^^
108+
return p as any;
109+
// ^^^^^^
110+
}
111+
112+
/**/declare enum E1 { }
113+
// ^^^^^^^^^^^^^^^^^^ `declare enum`
114+
115+
void 0;
116+
117+
/**/declare namespace N { }
118+
// ^^^^^^^^^^^^^^^^^^^^^^ `declare namespace`
119+
120+
void 0;
121+
122+
/**/declare module M { }
123+
// ^^^^^^^^^^^^^^^^^^^ `declare module`
124+
125+
void 0;
126+
127+
/**/declare let a;
128+
// ^^^^^^^^^^^^^^ `declare let`
129+
130+
void 0;
131+
132+
/**/declare class DeclaredClass { }
133+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `declare class`
134+
135+
void 0;
136+
137+
/**/declare function DeclaredFunction(): void;
138+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `declare function`
139+
140+
void 0;
141+
142+
// `=>` spanning line cases:
143+
{
144+
()
145+
: any =>
146+
1
147+
};
148+
{
149+
():
150+
any =>
151+
1
152+
};
153+
{
154+
(
155+
)
156+
: any =>
157+
1
158+
};
159+
{
160+
(
161+
): (
162+
| any
163+
) =>
164+
1
165+
};
166+
{
167+
(
168+
):
169+
NonNullable<any
170+
> =>
171+
1
172+
};
173+
{
174+
(a, b, c: D = [] as any/*comment-1*/)/*comment-2*/:
175+
any =>
176+
1
177+
};
178+
179+
180+
():
181+
any =>
182+
1;
183+
184+
{
185+
(a, b, c: D = [] as any/*comment-1*/)/*comment-2*/:
186+
/*comment-3*/any/*comment-4*/ =>
187+
1
188+
};
189+
190+
type 任意の型 = any;
191+
192+
():
193+
任意の型 =>
194+
1;
195+
196+
()/*comment-1*/:/*comment-2*/
197+
/*comment-3*/任意の型/*comment-4*/ =>
198+
1;

0 commit comments

Comments
 (0)
Please sign in to comment.