2021-05-06 08:56:54 +03:00
|
|
|
#![feature(test)]
|
2021-08-13 12:39:13 +03:00
|
|
|
#![feature(bench_black_box)]
|
2021-05-06 08:56:54 +03:00
|
|
|
|
|
|
|
extern crate test;
|
|
|
|
|
2021-08-13 13:03:04 +03:00
|
|
|
use std::{hint::black_box, io::stderr, sync::Arc};
|
|
|
|
use swc_babel_compat::babelify::{Babelify, Context};
|
|
|
|
use swc_common::{errors::Handler, FileName, FilePathMapping, SourceFile, SourceMap};
|
2021-05-06 08:56:54 +03:00
|
|
|
use swc_ecma_ast::Program;
|
|
|
|
use swc_ecma_parser::{JscTarget, Syntax};
|
2021-08-13 13:03:04 +03:00
|
|
|
use swc_ecma_transforms::{compat::es2020, typescript};
|
2021-05-06 08:56:54 +03:00
|
|
|
use swc_ecma_visit::FoldWith;
|
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
static SOURCE: &str = include_str!("assets/AjaxObservable.ts");
|
|
|
|
|
|
|
|
fn mk() -> swc::Compiler {
|
|
|
|
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
|
2021-08-13 10:05:40 +03:00
|
|
|
let c = swc::Compiler::new(cm.clone());
|
2021-05-06 08:56:54 +03:00
|
|
|
|
|
|
|
c
|
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
|
|
|
|
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-05-06 08:56:54 +03:00
|
|
|
JscTarget::Es5,
|
|
|
|
Syntax::Typescript(Default::default()),
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
(fm, module)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn babelify_only(b: &mut Bencher) {
|
|
|
|
b.bytes = SOURCE.len() as _;
|
|
|
|
|
|
|
|
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, || {
|
2021-05-06 08:56:54 +03:00
|
|
|
module
|
|
|
|
.fold_with(&mut typescript::strip())
|
2021-09-27 11:03:48 +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) {
|
|
|
|
b.bytes = src.len() as _;
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// https://esprima.org/test/compare.html
|
|
|
|
macro_rules! src_to_babel_ast {
|
|
|
|
($name:ident, $src:expr) => {
|
|
|
|
#[bench]
|
|
|
|
fn $name(b: &mut Bencher) {
|
|
|
|
parse_and_babelify(b, stringify!($name), $src);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_angular,
|
2021-08-19 11:21:08 +03:00
|
|
|
include_str!("../../../parser/benches/files/angular-1.2.5.js")
|
2021-05-06 08:56:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_backbone,
|
2021-08-19 11:21:08 +03:00
|
|
|
include_str!("../../../parser/benches/files/backbone-1.1.0.js")
|
2021-05-06 08:56:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_jquery,
|
2021-08-19 11:21:08 +03:00
|
|
|
include_str!("../../../parser/benches/files/jquery-1.9.1.js")
|
2021-05-06 08:56:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_jquery_mobile,
|
2021-08-19 11:21:08 +03:00
|
|
|
include_str!("../../../parser/benches/files/jquery.mobile-1.4.2.js")
|
2021-05-06 08:56:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_mootools,
|
2021-08-19 11:21:08 +03:00
|
|
|
include_str!("../../../parser/benches/files/mootools-1.4.5.js")
|
2021-05-06 08:56:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_underscore,
|
2021-08-19 11:21:08 +03:00
|
|
|
include_str!("../../../parser/benches/files/underscore-1.5.2.js")
|
2021-05-06 08:56:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
src_to_babel_ast!(
|
|
|
|
parse_and_babelify_yui,
|
2021-08-19 11:21:08 +03:00
|
|
|
include_str!("../../../parser/benches/files/yui-3.12.0.js")
|
2021-05-06 08:56:54 +03:00
|
|
|
);
|