2022-03-12 09:16:02 +03:00
|
|
|
//! Ported from https://github.com/privatenumber/minification-benchmarks
|
|
|
|
|
2023-11-11 22:10:56 +03:00
|
|
|
extern crate swc_malloc;
|
2022-04-17 09:35:36 +03:00
|
|
|
|
2022-03-12 09:16:02 +03:00
|
|
|
use std::fs::read_to_string;
|
|
|
|
|
2024-07-10 09:20:27 +03:00
|
|
|
use codspeed_criterion_compat::{black_box, criterion_group, criterion_main, Criterion};
|
2024-07-20 04:26:10 +03:00
|
|
|
use swc_allocator::Allocator;
|
2022-09-01 07:36:30 +03:00
|
|
|
use swc_common::{errors::HANDLER, sync::Lrc, FileName, Mark, SourceMap};
|
2024-10-29 05:25:16 +03:00
|
|
|
use swc_ecma_ast::Program;
|
2022-03-12 09:16:02 +03:00
|
|
|
use swc_ecma_codegen::text_writer::JsWriter;
|
|
|
|
use swc_ecma_minifier::{
|
|
|
|
optimize,
|
2023-03-30 11:06:02 +03:00
|
|
|
option::{ExtraOptions, MangleOptions, MinifyOptions},
|
2022-03-12 09:16:02 +03:00
|
|
|
};
|
|
|
|
use swc_ecma_parser::parse_file_as_module;
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_ecma_transforms_base::{fixer::fixer, resolver};
|
2022-03-12 09:16:02 +03:00
|
|
|
|
|
|
|
pub fn bench_files(c: &mut Criterion) {
|
2024-07-10 09:20:27 +03:00
|
|
|
let mut group = c.benchmark_group("es/minifier/libs");
|
2022-03-12 09:16:02 +03:00
|
|
|
group.sample_size(10);
|
|
|
|
|
|
|
|
let mut bench_file = |name: &str| {
|
2023-03-14 07:56:21 +03:00
|
|
|
let src = read_to_string(format!("benches/full/{}.js", name)).unwrap();
|
2022-03-12 09:16:02 +03:00
|
|
|
|
2024-07-23 08:31:35 +03:00
|
|
|
group.bench_function(format!("es/minifier/libs/{}", name), |b| {
|
2022-03-12 09:16:02 +03:00
|
|
|
b.iter(|| {
|
|
|
|
// We benchmark full time, including time for creating cm, handler
|
2024-07-20 04:26:10 +03:00
|
|
|
let allocator = Allocator::default();
|
|
|
|
let _guard = unsafe { allocator.guard() };
|
|
|
|
|
2022-03-12 09:16:02 +03:00
|
|
|
run(&src)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
bench_file("antd");
|
|
|
|
bench_file("d3");
|
|
|
|
bench_file("echarts");
|
|
|
|
bench_file("jquery");
|
|
|
|
bench_file("lodash");
|
|
|
|
bench_file("moment");
|
|
|
|
bench_file("react");
|
|
|
|
bench_file("terser");
|
|
|
|
bench_file("three");
|
|
|
|
bench_file("typescript");
|
|
|
|
bench_file("victory");
|
|
|
|
bench_file("vue");
|
|
|
|
}
|
|
|
|
|
|
|
|
criterion_group!(files, bench_files);
|
|
|
|
criterion_main!(files);
|
|
|
|
|
|
|
|
fn run(src: &str) {
|
|
|
|
testing::run_test2(false, |cm, handler| {
|
2022-09-01 07:36:30 +03:00
|
|
|
HANDLER.set(&handler, || {
|
2024-07-09 04:24:45 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon.into(), src.into());
|
2022-09-01 07:36:30 +03:00
|
|
|
|
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
|
|
|
|
|
|
|
let program = parse_file_as_module(
|
|
|
|
&fm,
|
|
|
|
Default::default(),
|
|
|
|
Default::default(),
|
|
|
|
None,
|
2024-07-19 07:22:34 +03:00
|
|
|
&mut Vec::new(),
|
2022-09-01 07:36:30 +03:00
|
|
|
)
|
|
|
|
.map_err(|err| {
|
|
|
|
err.into_diagnostic(&handler).emit();
|
|
|
|
})
|
2024-10-29 05:25:16 +03:00
|
|
|
.map(Program::Module)
|
|
|
|
.map(|module| module.apply(resolver(unresolved_mark, top_level_mark, false)))
|
2022-09-01 07:36:30 +03:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let output = optimize(
|
2024-10-29 05:25:16 +03:00
|
|
|
program,
|
2022-09-01 07:36:30 +03:00
|
|
|
cm.clone(),
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
&MinifyOptions {
|
|
|
|
rename: false,
|
2023-03-30 11:06:02 +03:00
|
|
|
compress: Some(Default::default()),
|
2022-09-01 07:36:30 +03:00
|
|
|
mangle: Some(MangleOptions {
|
|
|
|
props: None,
|
2023-01-10 09:42:49 +03:00
|
|
|
top_level: Some(true),
|
2022-09-01 07:36:30 +03:00
|
|
|
keep_class_names: false,
|
|
|
|
keep_fn_names: false,
|
|
|
|
keep_private_props: false,
|
|
|
|
ie8: false,
|
2023-08-09 23:42:12 +03:00
|
|
|
..Default::default()
|
2022-09-01 07:36:30 +03:00
|
|
|
}),
|
|
|
|
wrap: false,
|
|
|
|
enclose: false,
|
|
|
|
},
|
|
|
|
&ExtraOptions {
|
|
|
|
unresolved_mark,
|
|
|
|
top_level_mark,
|
2024-08-26 02:42:53 +03:00
|
|
|
mangle_name_cache: None,
|
2022-09-01 07:36:30 +03:00
|
|
|
},
|
2024-10-29 05:25:16 +03:00
|
|
|
);
|
2022-09-01 07:36:30 +03:00
|
|
|
|
2024-10-29 05:25:16 +03:00
|
|
|
let output = output.apply(fixer(None));
|
2022-09-01 07:36:30 +03:00
|
|
|
|
|
|
|
let code = print(cm, &[output], true);
|
|
|
|
|
|
|
|
black_box(code);
|
|
|
|
Ok(())
|
2022-03-12 09:16:02 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print<N: swc_ecma_codegen::Node>(cm: Lrc<SourceMap>, nodes: &[N], minify: bool) -> String {
|
2024-07-19 07:22:34 +03:00
|
|
|
let mut buf = Vec::new();
|
2022-03-12 09:16:02 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut emitter = swc_ecma_codegen::Emitter {
|
2023-09-05 15:24:25 +03:00
|
|
|
cfg: swc_ecma_codegen::Config::default().with_minify(minify),
|
2022-03-12 09:16:02 +03:00
|
|
|
cm: cm.clone(),
|
|
|
|
comments: None,
|
|
|
|
wr: Box::new(JsWriter::new(cm, "\n", &mut buf, None)),
|
|
|
|
};
|
|
|
|
|
|
|
|
for n in nodes {
|
|
|
|
n.emit_with(&mut emitter).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String::from_utf8(buf).unwrap()
|
|
|
|
}
|