2022-04-03 18:56:29 +03:00
|
|
|
use std::{io::stderr, sync::Arc};
|
2022-02-09 09:33:32 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
|
2021-11-16 13:31:02 +03:00
|
|
|
use swc::config::IsModule;
|
2021-12-04 05:35:16 +03:00
|
|
|
use swc_common::{errors::Handler, FileName, FilePathMapping, Mark, SourceFile, SourceMap};
|
2021-10-31 15:25:15 +03:00
|
|
|
use swc_ecma_ast::{EsVersion, Program};
|
|
|
|
use swc_ecma_parser::Syntax;
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_ecma_transforms::{compat::es2020, resolver, typescript};
|
2021-05-06 08:56:54 +03:00
|
|
|
use swc_ecma_visit::FoldWith;
|
2021-11-23 23:34:41 +03:00
|
|
|
use swc_estree_compat::babelify::{Babelify, Context};
|
2021-05-06 08:56:54 +03:00
|
|
|
|
|
|
|
static SOURCE: &str = include_str!("assets/AjaxObservable.ts");
|
|
|
|
|
|
|
|
fn mk() -> swc::Compiler {
|
|
|
|
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
|
2022-01-13 17:06:11 +03:00
|
|
|
swc::Compiler::new(cm)
|
2021-05-06 08:56:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(c: &swc::Compiler, src: &str) -> (Arc<SourceFile>, Program) {
|
|
|
|
let fm = c.cm.new_source_file(
|
|
|
|
FileName::Real("rxjs/src/internal/observable/dom/AjaxObservable.ts".into()),
|
|
|
|
src.to_string(),
|
|
|
|
);
|
2021-08-13 10:05:40 +03:00
|
|
|
|
|
|
|
let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
|
|
|
|
|
2022-03-04 15:07:51 +03:00
|
|
|
let comments = c.comments().clone();
|
2021-05-06 08:56:54 +03:00
|
|
|
let module = c
|
|
|
|
.parse_js(
|
|
|
|
fm.clone(),
|
2021-08-13 10:05:40 +03:00
|
|
|
&handler,
|
2021-10-31 15:25:15 +03:00
|
|
|
EsVersion::Es5,
|
2021-05-06 08:56:54 +03:00
|
|
|
Syntax::Typescript(Default::default()),
|
2021-11-16 13:31:02 +03:00
|
|
|
IsModule::Bool(true),
|
2022-03-04 15:07:51 +03:00
|
|
|
Some(&comments),
|
2021-05-06 08:56:54 +03:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
(fm, module)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn babelify_only(b: &mut Bencher) {
|
|
|
|
let c = mk();
|
|
|
|
let (fm, module) = parse(&c, SOURCE);
|
2021-08-13 10:05:40 +03:00
|
|
|
let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
|
|
|
|
|
|
|
|
let module = c.run_transform(&handler, false, || {
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
|
|
|
|
2021-05-06 08:56:54 +03:00
|
|
|
module
|
2022-04-26 10:38:50 +03:00
|
|
|
.fold_with(&mut resolver(unresolved_mark, top_level_mark, true))
|
|
|
|
.fold_with(&mut typescript::strip(top_level_mark))
|
2021-11-15 08:31:21 +03:00
|
|
|
.fold_with(&mut es2020(Default::default()))
|
2021-05-06 08:56:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
let program = module.clone();
|
|
|
|
let ctx = Context {
|
|
|
|
fm: fm.clone(),
|
|
|
|
cm: c.cm.clone(),
|
|
|
|
comments: c.comments().clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let babel_ast = program.babelify(&ctx);
|
|
|
|
black_box(babel_ast)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_and_babelify(b: &mut Bencher, _name: &str, src: &str) {
|
|
|
|
let c = mk();
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
let (fm, program) = parse(&c, src);
|
|
|
|
let ctx = Context {
|
|
|
|
fm,
|
|
|
|
cm: c.cm.clone(),
|
|
|
|
comments: c.comments().clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let babel_ast = program.babelify(&ctx);
|
|
|
|
black_box(babel_ast);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
fn bench_cases(c: &mut Criterion) {
|
2022-04-17 21:36:15 +03:00
|
|
|
c.bench_function("babelify-only", babelify_only);
|
2022-04-03 18:56:29 +03:00
|
|
|
|
|
|
|
/// https://esprima.org/test/compare.html
|
|
|
|
macro_rules! src_to_babel_ast {
|
|
|
|
($name:ident, $src:expr) => {
|
|
|
|
c.bench_function(stringify!($name), |b| {
|
|
|
|
parse_and_babelify(b, stringify!($name), $src)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_angular,
|
|
|
|
include_str!("../../swc_ecma_parser/benches/files/angular-1.2.5.js")
|
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_backbone,
|
|
|
|
include_str!("../../swc_ecma_parser/benches/files/backbone-1.1.0.js")
|
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_jquery,
|
|
|
|
include_str!("../../swc_ecma_parser/benches/files/jquery-1.9.1.js")
|
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_jquery_mobile,
|
|
|
|
include_str!("../../swc_ecma_parser/benches/files/jquery.mobile-1.4.2.js")
|
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_mootools,
|
|
|
|
include_str!("../../swc_ecma_parser/benches/files/mootools-1.4.5.js")
|
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_underscore,
|
|
|
|
include_str!("../../swc_ecma_parser/benches/files/underscore-1.5.2.js")
|
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_yui,
|
|
|
|
include_str!("../../swc_ecma_parser/benches/files/yui-3.12.0.js")
|
|
|
|
);
|
2021-05-06 08:56:54 +03:00
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
criterion_group!(benches, bench_cases);
|
|
|
|
criterion_main!(benches);
|