Skip to content

Commit

Permalink
perf(es/typescript): Add a benchmark for fast TS strip (#9205)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jul 11, 2024
1 parent b0a3987 commit 7d9364c
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion crates/swc_fast_ts_strip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
31 changes: 31 additions & 0 deletions crates/swc_fast_ts_strip/benches/assets.rs
Original file line number Diff line number Diff line change
@@ -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);
198 changes: 198 additions & 0 deletions crates/swc_fast_ts_strip/benches/assets/test.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 7d9364c

Please sign in to comment.