Skip to content

Commit efbb82b

Browse files
committed
ci(benchmarks): add benchmark for updating tokens for raw transfer
1 parent f78e6df commit efbb82b

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

tasks/benchmark/benches/parser.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,65 @@ fn bench_estree_tokens(criterion: &mut Criterion) {
160160
group.finish();
161161
}
162162

163-
criterion_group!(parser, bench_parser, bench_parser_tokens, bench_estree, bench_estree_tokens);
163+
fn bench_estree_tokens_raw(criterion: &mut Criterion) {
164+
let mut group = criterion.benchmark_group("estree_tokens_raw");
165+
166+
for file in TestFiles::complicated().files().iter().take(1) {
167+
let id = BenchmarkId::from_parameter(&file.file_name);
168+
let source_text = &file.source_text;
169+
let source_type = file.source_type;
170+
let mut allocator = Allocator::default();
171+
172+
group.bench_function(id, |b| {
173+
b.iter_with_setup_wrapper(|runner| {
174+
allocator.reset();
175+
176+
// Use `RuntimeParserConfig` (runtime config), same as NAPI parser package will.
177+
// `bench_estree` uses `NoTokensParserConfig` (implicitly as default).
178+
// Usually it's inadvisable to use 2 different configs in the same application,
179+
// but this is just a benchmark, and it's better if we don't entwine this benchmark with `bench_estree`.
180+
let config = RuntimeParserConfig::new(true);
181+
182+
let ret = Parser::new(&allocator, source_text, source_type)
183+
.with_options(ParseOptions {
184+
parse_regular_expression: true,
185+
..ParseOptions::default()
186+
})
187+
.with_config(config)
188+
.parse();
189+
let ParserReturn { program, tokens, .. } = ret;
190+
191+
// Creating span converter is not performed in measured section, as we only want to measure tokens.
192+
// Span converter needs to be created anyway for converting spans in AST.
193+
let span_converter = Utf8ToUtf16::new(program.source_text);
194+
195+
runner.run(|| {
196+
let tokens_json = to_estree_tokens_json(
197+
&tokens,
198+
&program,
199+
program.source_text,
200+
&span_converter,
201+
ESTreeTokenOptionsJS,
202+
);
203+
let tokens_json = black_box(tokens_json);
204+
// Allocate tokens JSON into arena, same as linter and NAPI parser package do
205+
let _tokens_json = allocator.alloc_str(&tokens_json);
206+
207+
program
208+
});
209+
});
210+
});
211+
}
212+
213+
group.finish();
214+
}
215+
216+
criterion_group!(
217+
parser,
218+
bench_parser,
219+
bench_parser_tokens,
220+
bench_estree,
221+
bench_estree_tokens,
222+
bench_estree_tokens_raw
223+
);
164224
criterion_main!(parser);

0 commit comments

Comments
 (0)