2021-09-29 15:10:38 +03:00
|
|
|
extern crate swc_node_base;
|
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};
|
2018-12-21 10:54:36 +03:00
|
|
|
use swc_common::FileName;
|
2019-12-02 11:10:06 +03:00
|
|
|
use swc_ecma_codegen::{self, Emitter};
|
2020-07-31 12:49:07 +03:00
|
|
|
use swc_ecma_parser::{Parser, StringInput, Syntax};
|
2018-12-21 10:54:36 +03:00
|
|
|
|
2020-03-01 14:05:03 +03:00
|
|
|
const COLORS_JS: &str = r#"
|
2018-12-21 10:54:36 +03:00
|
|
|
'use strict';
|
|
|
|
/**
|
|
|
|
* Extract red color out of a color integer:
|
|
|
|
*
|
|
|
|
* 0x00DEAD -> 0x00
|
|
|
|
*
|
|
|
|
* @param {Number} color
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
function red( color )
|
|
|
|
{
|
|
|
|
let foo = 3.14;
|
|
|
|
return color >> 16;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Extract green out of a color integer:
|
|
|
|
*
|
|
|
|
* 0x00DEAD -> 0xDE
|
|
|
|
*
|
|
|
|
* @param {Number} color
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
function green( color )
|
|
|
|
{
|
|
|
|
return ( color >> 8 ) & 0xFF;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Extract blue color out of a color integer:
|
|
|
|
*
|
|
|
|
* 0x00DEAD -> 0xAD
|
|
|
|
*
|
|
|
|
* @param {Number} color
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
function blue( color )
|
|
|
|
{
|
|
|
|
return color & 0xFF;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Converts an integer containing a color such as 0x00DEAD to a hex
|
|
|
|
* string, such as '#00DEAD';
|
|
|
|
*
|
|
|
|
* @param {Number} int
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
function intToHex( int )
|
|
|
|
{
|
|
|
|
const mask = '#000000';
|
|
|
|
const hex = int.toString( 16 );
|
|
|
|
return mask.substring( 0, 7 - hex.length ) + hex;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Converts a hex string containing a color such as '#00DEAD' to
|
|
|
|
* an integer, such as 0x00DEAD;
|
|
|
|
*
|
|
|
|
* @param {Number} num
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
function hexToInt( hex )
|
|
|
|
{
|
|
|
|
return parseInt( hex.substring( 1 ), 16 );
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
|
|
red,
|
|
|
|
green,
|
|
|
|
blue,
|
|
|
|
intToHex,
|
|
|
|
hexToInt,
|
|
|
|
};
|
|
|
|
"#;
|
|
|
|
|
2020-03-01 14:05:03 +03:00
|
|
|
const LARGE_PARTIAL_JS: &str = include_str!("large-partial.js");
|
|
|
|
|
|
|
|
fn bench_emitter(b: &mut Bencher, s: &str) {
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(true, |cm, handler| {
|
2020-03-01 14:05:03 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, s.into());
|
2020-07-31 12:49:07 +03:00
|
|
|
let mut parser = Parser::new(Syntax::default(), StringInput::from(&*fm), None);
|
2020-07-25 14:26:04 +03:00
|
|
|
|
2020-03-02 14:49:08 +03:00
|
|
|
let mut src_map_buf = vec![];
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2020-07-25 14:26:04 +03:00
|
|
|
.map_err(|e| e.into_diagnostic(handler).emit())
|
2019-01-07 13:43:47 +03:00
|
|
|
.unwrap();
|
2018-12-21 10:54:36 +03:00
|
|
|
|
2020-07-25 14:26:04 +03:00
|
|
|
for err in parser.take_errors() {
|
|
|
|
err.into_diagnostic(handler).emit();
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:54:36 +03:00
|
|
|
b.iter(|| {
|
2020-03-02 14:49:08 +03:00
|
|
|
let mut buf = vec![];
|
2018-12-21 10:54:36 +03:00
|
|
|
{
|
|
|
|
let mut emitter = Emitter {
|
|
|
|
cfg: swc_ecma_codegen::Config {
|
|
|
|
..Default::default()
|
|
|
|
},
|
2019-02-26 07:56:58 +03:00
|
|
|
comments: None,
|
2018-12-21 10:54:36 +03:00
|
|
|
cm: cm.clone(),
|
2021-09-29 15:10:38 +03:00
|
|
|
wr: swc_ecma_codegen::text_writer::JsWriter::new(
|
2018-12-21 10:54:36 +03:00
|
|
|
cm.clone(),
|
|
|
|
"\n",
|
2020-03-02 14:49:08 +03:00
|
|
|
&mut buf,
|
|
|
|
Some(&mut src_map_buf),
|
2021-09-29 15:10:38 +03:00
|
|
|
),
|
2018-12-21 10:54:36 +03:00
|
|
|
};
|
|
|
|
|
2020-03-02 14:49:08 +03:00
|
|
|
let _ = emitter.emit_module(&module);
|
2018-12-21 10:54:36 +03:00
|
|
|
}
|
2020-03-02 14:49:08 +03:00
|
|
|
black_box(buf);
|
|
|
|
let srcmap = cm.build_source_map(&mut src_map_buf);
|
|
|
|
black_box(srcmap);
|
2018-12-21 10:54:36 +03:00
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
fn bench_cases(c: &mut Criterion) {
|
|
|
|
c.bench_function("colors", |b| bench_emitter(b, COLORS_JS));
|
|
|
|
c.bench_function("large", |b| bench_emitter(b, LARGE_PARTIAL_JS));
|
2020-03-01 14:05:03 +03:00
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
criterion_group!(benches, bench_cases);
|
|
|
|
criterion_main!(benches);
|