2022-04-03 18:56:29 +03:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
|
2021-05-08 09:47:20 +03:00
|
|
|
use swc_common::{comments::SingleThreadedComments, FileName, Mark};
|
2020-07-31 12:49:07 +03:00
|
|
|
use swc_ecma_parser::{Parser, StringInput, Syntax};
|
2019-12-28 05:25:54 +03:00
|
|
|
use swc_ecma_preset_env::{preset_env, Config};
|
2022-12-06 15:24:55 +03:00
|
|
|
use swc_ecma_transforms::helpers::{Helpers, HELPERS};
|
2020-07-23 20:18:22 +03:00
|
|
|
use swc_ecma_visit::FoldWith;
|
2019-12-28 05:25:54 +03:00
|
|
|
|
|
|
|
fn run(b: &mut Bencher, src: &str, config: Config) {
|
|
|
|
let _ = ::testing::run_test(false, |cm, handler| {
|
2022-12-06 15:24:55 +03:00
|
|
|
HELPERS.set(&Helpers::new(true), || {
|
|
|
|
let fm = cm.new_source_file(FileName::Anon, src.into());
|
2019-12-28 05:25:54 +03:00
|
|
|
|
2022-12-06 15:24:55 +03:00
|
|
|
let mut parser = Parser::new(Syntax::default(), StringInput::from(&*fm), None);
|
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
|
|
|
.map_err(|e| e.into_diagnostic(handler).emit())
|
|
|
|
.unwrap();
|
2020-07-25 14:26:04 +03:00
|
|
|
|
2022-12-06 15:24:55 +03:00
|
|
|
for e in parser.take_errors() {
|
|
|
|
e.into_diagnostic(handler).emit()
|
|
|
|
}
|
2020-07-25 14:26:04 +03:00
|
|
|
|
2022-12-06 15:24:55 +03:00
|
|
|
let mut folder = preset_env(
|
|
|
|
Mark::fresh(Mark::root()),
|
|
|
|
Some(SingleThreadedComments::default()),
|
|
|
|
config,
|
|
|
|
Default::default(),
|
|
|
|
&mut Default::default(),
|
|
|
|
);
|
2019-12-28 05:25:54 +03:00
|
|
|
|
2022-12-06 15:24:55 +03:00
|
|
|
b.iter(|| black_box(module.clone().fold_with(&mut folder)));
|
|
|
|
Ok(())
|
|
|
|
})
|
2019-12-28 05:25:54 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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("es/preset-env/usage/builtin_type", |b| {
|
2022-04-03 18:56:29 +03:00
|
|
|
const SOURCE: &str = r#"
|
|
|
|
// From a length
|
|
|
|
var float32 = new Float32Array(2);
|
|
|
|
float32[0] = 42;
|
|
|
|
console.log(float32[0]); // 42
|
|
|
|
console.log(float32.length); // 2
|
|
|
|
console.log(float32.BYTES_PER_ELEMENT); // 4
|
|
|
|
|
|
|
|
// From an array
|
|
|
|
var arr = new Float32Array([21,31]);
|
|
|
|
console.log(arr[1]); // 31
|
|
|
|
|
|
|
|
// From another TypedArray
|
|
|
|
var x = new Float32Array([21, 31]);
|
|
|
|
var y = new Float32Array(x);
|
|
|
|
console.log(y[0]); // 21
|
|
|
|
|
|
|
|
// From an ArrayBuffer
|
|
|
|
var buffer = new ArrayBuffer(16);
|
|
|
|
var z = new Float32Array(buffer, 0, 4);
|
|
|
|
|
|
|
|
// From an iterable
|
|
|
|
var iterable = function*(){ yield* [1,2,3]; }();
|
|
|
|
var float32 = new Float32Array(iterable);
|
|
|
|
// Float32Array[1, 2, 3]
|
|
|
|
"#;
|
|
|
|
|
|
|
|
run(b, SOURCE, Default::default())
|
|
|
|
});
|
2019-12-28 05:25:54 +03:00
|
|
|
|
2022-04-17 21:36:15 +03:00
|
|
|
c.bench_function("es/preset-env/usage/property", |b| {
|
2022-04-03 18:56:29 +03:00
|
|
|
const SOURCE: &str = r#"
|
|
|
|
const target = { a: 1, b: 2 };
|
|
|
|
const source = { b: 4, c: 5 };
|
|
|
|
|
|
|
|
const returnedTarget = Object.assign(target, source);
|
|
|
|
|
|
|
|
console.log(target);
|
|
|
|
// expected output: Object { a: 1, b: 4, c: 5 }
|
|
|
|
|
|
|
|
console.log(returnedTarget);
|
|
|
|
// expected output: Object { a: 1, b: 4, c: 5 }
|
|
|
|
"#;
|
|
|
|
|
|
|
|
run(b, SOURCE, Default::default())
|
|
|
|
});
|
2019-12-28 05:25:54 +03:00
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
criterion_group!(benches, bench_cases);
|
|
|
|
criterion_main!(benches);
|