swc/ecmascript/codegen/examples/gen.rs
Donny/강동윤 4c983e9158
perf(es/codegen, es/parser): Improve performance (#2406)
ast_node:
 - Make `.span()` inlinable.

swc_ecma_codegen:
 - Reduce call to *heavy* `.span()` calls.

swc_ecma_parser:
 - Reduce `memmove` by using typed-arena.

swc_bundler:
 - Make `Bundle.bundle` take `&mut self`.
 - Make drop concurrent.
2021-10-15 12:49:37 +09:00

62 lines
1.7 KiB
Rust

/// Use memory allocator
extern crate swc_node_base;
use std::{env, fs, path::Path, time::Instant};
use swc_common::input::SourceFileInput;
use swc_ecma_ast::*;
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
fn do_test(entry: &Path) {
testing::run_test2(false, |cm, _| {
let fm = cm.load_file(entry).unwrap();
let lexer = Lexer::new(
Syntax::Typescript(Default::default()),
EsVersion::latest(),
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let m = parser
.parse_module()
.expect("failed to parse input as a module");
for _ in 0..100 {
let code = {
let mut buf = vec![];
{
let mut emitter = Emitter {
cfg: swc_ecma_codegen::Config {
..Default::default()
},
cm: cm.clone(),
comments: None,
wr: JsWriter::new(cm.clone(), "\n", &mut buf, None),
};
emitter.emit_module(&m).unwrap();
}
String::from_utf8_lossy(&buf).to_string()
};
fs::write("output.js", &code).unwrap();
}
Ok(())
})
.expect("failed to process a module");
}
/// Usage: ./scripts/instruements path/to/input/file
fn main() {
let main_file = env::args().nth(1).unwrap();
let start = Instant::now();
do_test(Path::new(&main_file));
let dur = start.elapsed();
println!("Took {:?}", dur);
}