1
+ use std:: path:: Path ;
2
+
1
3
use criterion:: { measurement:: WallTime , * } ;
2
4
use rayon:: prelude:: * ;
3
5
@@ -9,23 +11,25 @@ trait TheBencher {
9
11
10
12
const ID : & ' static str ;
11
13
12
- fn parse ( source : & str ) -> Self :: ParseOutput ;
14
+ fn parse ( filename : & Path , source : & str ) -> Self :: ParseOutput ;
13
15
14
- fn bench ( g : & mut BenchmarkGroup < ' _ , WallTime > , source : & str ) {
16
+ fn bench ( g : & mut BenchmarkGroup < ' _ , WallTime > , path : & Path , source : & str ) {
15
17
let cpus = num_cpus:: get_physical ( ) ;
16
18
let id = BenchmarkId :: new ( Self :: ID , "single-thread" ) ;
17
- g. bench_with_input ( id, & source, |b, source| b. iter ( || Self :: parse ( source) ) ) ;
19
+ g. bench_with_input ( id, & source, |b, source| {
20
+ b. iter ( || Self :: parse ( path, source) )
21
+ } ) ;
18
22
19
23
let id = BenchmarkId :: new ( Self :: ID , "no-drop" ) ;
20
24
g. bench_with_input ( id, & source, |b, source| {
21
- b. iter_with_large_drop ( || Self :: parse ( source) )
25
+ b. iter_with_large_drop ( || Self :: parse ( path , source) )
22
26
} ) ;
23
27
24
28
let id = BenchmarkId :: new ( Self :: ID , "parallel" ) ;
25
29
g. bench_with_input ( id, & source, |b, source| {
26
30
b. iter ( || {
27
31
( 0 ..cpus) . into_par_iter ( ) . for_each ( |_| {
28
- Self :: parse ( source) ;
32
+ Self :: parse ( path , source) ;
29
33
} ) ;
30
34
} )
31
35
} ) ;
@@ -39,9 +43,9 @@ impl TheBencher for OxcBencher {
39
43
40
44
const ID : & ' static str = "oxc" ;
41
45
42
- fn parse ( source : & str ) -> Self :: ParseOutput {
46
+ fn parse ( path : & Path , source : & str ) -> Self :: ParseOutput {
43
47
let allocator = oxc:: allocator:: Allocator :: default ( ) ;
44
- let source_type = oxc:: span:: SourceType :: default ( ) ;
48
+ let source_type = oxc:: span:: SourceType :: from_path ( path ) . unwrap ( ) ;
45
49
_ = oxc:: parser:: Parser :: new ( & allocator, source, source_type) . parse ( ) ;
46
50
allocator
47
51
}
@@ -54,10 +58,18 @@ impl TheBencher for SwcBencher {
54
58
55
59
const ID : & ' static str = "swc" ;
56
60
57
- fn parse ( source : & str ) -> Self :: ParseOutput {
58
- use swc_ecma_parser:: { Parser , StringInput , Syntax } ;
61
+ fn parse ( path : & Path , source : & str ) -> Self :: ParseOutput {
62
+ use swc_ecma_parser:: { EsConfig , Parser , StringInput , Syntax , TsConfig } ;
63
+ let syntax = match path. extension ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) {
64
+ "js" => Syntax :: Es ( EsConfig :: default ( ) ) ,
65
+ "tsx" => Syntax :: Typescript ( TsConfig {
66
+ tsx : true ,
67
+ ..TsConfig :: default ( )
68
+ } ) ,
69
+ _ => panic ! ( "need to define syntax for swc" ) ,
70
+ } ;
59
71
Parser :: new (
60
- Syntax :: Es ( Default :: default ( ) ) ,
72
+ syntax ,
61
73
StringInput :: new ( source, Default :: default ( ) , Default :: default ( ) ) ,
62
74
None ,
63
75
)
@@ -68,24 +80,28 @@ impl TheBencher for SwcBencher {
68
80
struct BiomeBencher ;
69
81
70
82
impl TheBencher for BiomeBencher {
71
- type ParseOutput = biome_js_parser:: Parse < biome_js_syntax:: JsModule > ;
83
+ type ParseOutput = biome_js_parser:: Parse < biome_js_syntax:: AnyJsRoot > ;
72
84
73
85
const ID : & ' static str = "biome" ;
74
86
75
- fn parse ( source : & str ) -> Self :: ParseOutput {
76
- biome_js_parser:: parse_module ( source, biome_js_parser:: JsParserOptions :: default ( ) )
87
+ fn parse ( path : & Path , source : & str ) -> Self :: ParseOutput {
88
+ let options = biome_js_parser:: JsParserOptions :: default ( ) ;
89
+ let source_type = biome_js_syntax:: JsFileSource :: try_from ( path) . unwrap ( ) ;
90
+ biome_js_parser:: parse ( source, source_type, options)
77
91
}
78
92
}
79
93
80
94
fn parser_benchmark ( c : & mut Criterion ) {
81
- let filename = "typescript.js" ;
82
- let source = std:: fs:: read_to_string ( filename) . unwrap ( ) ;
83
-
84
- let mut g = c. benchmark_group ( filename) ;
85
- OxcBencher :: bench ( & mut g, & source) ;
86
- SwcBencher :: bench ( & mut g, & source) ;
87
- BiomeBencher :: bench ( & mut g, & source) ;
88
- g. finish ( ) ;
95
+ let filenames = [ "typescript.js" , "cal.com.tsx" ] ;
96
+ for filename in filenames {
97
+ let path = Path :: new ( "files" ) . join ( filename) ;
98
+ let source = std:: fs:: read_to_string ( & path) . unwrap ( ) ;
99
+ let mut g = c. benchmark_group ( filename) ;
100
+ OxcBencher :: bench ( & mut g, & path, & source) ;
101
+ SwcBencher :: bench ( & mut g, & path, & source) ;
102
+ BiomeBencher :: bench ( & mut g, & path, & source) ;
103
+ g. finish ( ) ;
104
+ }
89
105
}
90
106
91
107
criterion_group ! ( parser, parser_benchmark) ;
0 commit comments