forked from swc-project/swc
-
Notifications
You must be signed in to change notification settings - Fork 4
/
with_parse.rs
133 lines (124 loc) · 3.12 KB
/
with_parse.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
extern crate swc_node_base;
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use swc_common::FileName;
use swc_ecma_codegen::{self, Emitter};
use swc_ecma_parser::{Parser, StringInput, Syntax};
const COLORS_JS: &str = r#"
'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,
};
"#;
const LARGE_PARTIAL_JS: &str = include_str!("large-partial.js");
fn bench_emitter(b: &mut Bencher, s: &str) {
let _ = ::testing::run_test(true, |cm, handler| {
b.iter(|| {
let fm = cm.new_source_file(FileName::Anon, s.into());
let mut parser = Parser::new(Syntax::default(), StringInput::from(&*fm), None);
let mut src_map_buf = vec![];
let module = parser
.parse_module()
.map_err(|e| e.into_diagnostic(handler).emit())
.unwrap();
for err in parser.take_errors() {
err.into_diagnostic(handler).emit();
}
let mut buf = vec![];
{
let mut emitter = Emitter {
cfg: swc_ecma_codegen::Config {
..Default::default()
},
comments: None,
cm: cm.clone(),
wr: Box::new(swc_ecma_codegen::text_writer::JsWriter::new(
cm.clone(),
"\n",
&mut buf,
Some(&mut src_map_buf),
)),
};
let _ = emitter.emit_module(&module);
}
black_box(buf);
let srcmap = cm.build_source_map(&src_map_buf);
black_box(srcmap);
});
Ok(())
});
}
fn bench_cases(c: &mut Criterion) {
c.bench_function("es/codegen/with-parser/colors", |b| {
bench_emitter(b, COLORS_JS)
});
c.bench_function("es/codegen/with-parser/large", |b| {
bench_emitter(b, LARGE_PARTIAL_JS)
});
}
criterion_group!(benches, bench_cases);
criterion_main!(benches);