2018-12-01 16:24:26 +03:00
|
|
|
#![feature(test)]
|
|
|
|
#![feature(specialization)]
|
|
|
|
use std::alloc::System;
|
|
|
|
|
|
|
|
#[global_allocator]
|
|
|
|
static GLOBAL: System = System;
|
2018-12-24 06:49:00 +03:00
|
|
|
#[macro_use]
|
2018-12-01 16:24:26 +03:00
|
|
|
extern crate swc_common;
|
|
|
|
extern crate swc_ecma_ast;
|
|
|
|
extern crate swc_ecma_parser;
|
|
|
|
extern crate swc_ecma_transforms;
|
|
|
|
extern crate test;
|
|
|
|
extern crate testing;
|
|
|
|
|
|
|
|
use swc_common::{FileName, FoldWith};
|
2018-12-30 05:57:27 +03:00
|
|
|
use swc_ecma_parser::{Parser, Session, SourceFileInput, Syntax};
|
2019-01-07 13:43:47 +03:00
|
|
|
use swc_ecma_transforms::{compat, helpers};
|
2018-12-01 16:24:26 +03:00
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
static SOURCE: &'static 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,
|
|
|
|
};"#;
|
|
|
|
|
|
|
|
/// Benchmark a folder
|
|
|
|
macro_rules! tr {
|
|
|
|
($b:expr, $tr:expr) => {
|
2018-12-21 10:54:36 +03:00
|
|
|
$b.bytes = SOURCE.len() as _;
|
|
|
|
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |cm, handler| {
|
2019-02-10 14:49:29 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
let mut parser = Parser::new(
|
2019-01-07 13:43:47 +03:00
|
|
|
Session { handler: &handler },
|
2019-01-08 10:34:35 +03:00
|
|
|
Syntax::default(),
|
2018-12-01 16:24:26 +03:00
|
|
|
SourceFileInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2019-01-17 17:17:16 +03:00
|
|
|
.map_err(|mut e| {
|
2019-01-07 13:43:47 +03:00
|
|
|
e.emit();
|
|
|
|
()
|
|
|
|
})
|
|
|
|
.unwrap();
|
2019-02-05 08:15:38 +03:00
|
|
|
helpers::HELPERS.set(&Default::default(), || {
|
|
|
|
let mut tr = $tr();
|
|
|
|
|
|
|
|
$b.iter(|| {
|
|
|
|
let module = module.clone();
|
|
|
|
test::black_box(module.fold_with(&mut tr))
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
})
|
2018-12-01 16:24:26 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-24 06:49:00 +03:00
|
|
|
#[bench]
|
|
|
|
fn all(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || chain!(
|
|
|
|
compat::es2017(),
|
2018-12-24 06:49:00 +03:00
|
|
|
compat::es2016(),
|
2019-02-05 08:15:38 +03:00
|
|
|
compat::es2015(),
|
2018-12-24 06:49:00 +03:00
|
|
|
compat::es3(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-12-26 11:09:48 +03:00
|
|
|
#[bench]
|
|
|
|
fn es2018(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2018());
|
2018-12-26 11:09:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2018_object_rest_spread(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2018::object_rest_spread());
|
2018-12-26 11:09:48 +03:00
|
|
|
}
|
|
|
|
|
2018-12-24 06:49:00 +03:00
|
|
|
#[bench]
|
|
|
|
fn es2017(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2017());
|
2018-12-24 06:49:00 +03:00
|
|
|
}
|
|
|
|
|
2018-12-26 11:09:48 +03:00
|
|
|
#[bench]
|
|
|
|
fn es2017_async_to_generator(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2017::async_to_generator());
|
2018-12-26 11:09:48 +03:00
|
|
|
}
|
|
|
|
|
2018-12-24 06:49:00 +03:00
|
|
|
#[bench]
|
|
|
|
fn es2016(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2016());
|
2018-12-24 06:49:00 +03:00
|
|
|
}
|
|
|
|
|
2018-12-26 11:09:48 +03:00
|
|
|
#[bench]
|
|
|
|
fn es2016_exponentation(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2016::exponentation());
|
2018-12-26 11:09:48 +03:00
|
|
|
}
|
|
|
|
|
2018-12-01 16:24:26 +03:00
|
|
|
#[bench]
|
|
|
|
fn es2015(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015());
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_arrow(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::arrow());
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
2018-12-26 11:09:48 +03:00
|
|
|
#[bench]
|
|
|
|
fn es2015_block_scoped_fn(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::BlockScopedFns);
|
2018-12-26 11:09:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_block_scoping(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::block_scoping());
|
2018-12-26 11:09:48 +03:00
|
|
|
}
|
|
|
|
|
2018-12-01 16:24:26 +03:00
|
|
|
#[bench]
|
|
|
|
fn es2015_classes(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::Classes);
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_computed_props(b: &mut Bencher) {
|
|
|
|
tr!(b, compat::es2015::computed_properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_destructuring(b: &mut Bencher) {
|
|
|
|
tr!(b, compat::es2015::destructuring);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_duplicate_keys(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::duplicate_keys());
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_parameters(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::parameters());
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2018-12-26 11:09:48 +03:00
|
|
|
fn es2015_fn_name(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::function_name());
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2018-12-26 11:09:48 +03:00
|
|
|
fn es2015_for_of(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::for_of());
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_instanceof(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::InstanceOf);
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_shorthand_property(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::Shorthand);
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_spread(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::Spread);
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_sticky_regex(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::StickyRegex);
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es2015_typeof_symbol(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es2015::TypeOfSymbol);
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
2018-12-26 11:09:48 +03:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn es3(b: &mut Bencher) {
|
2019-02-05 08:15:38 +03:00
|
|
|
tr!(b, || compat::es3());
|
2018-12-26 11:09:48 +03:00
|
|
|
}
|