|
| 1 | +#![feature(test)] |
| 2 | + |
| 3 | +extern crate test; |
| 4 | +extern crate ratel; |
| 5 | + |
| 6 | +use test::Bencher; |
| 7 | + |
| 8 | +static SOURCE: &'static str = r#" |
| 9 | +
|
| 10 | +'use strict'; |
| 11 | +
|
| 12 | +/** |
| 13 | + * Extranct red color out of a color integer: |
| 14 | + * |
| 15 | + * 0x00DEAD -> 0x00 |
| 16 | + * |
| 17 | + * @param {Number} color |
| 18 | + * @return {Number} |
| 19 | + */ |
| 20 | +function red( color ) |
| 21 | +{ |
| 22 | + let foo = 3.14; |
| 23 | + return color >> 16; |
| 24 | +} |
| 25 | +
|
| 26 | +/** |
| 27 | + * Extranct red color out of a color integer: |
| 28 | + * |
| 29 | + * 0x00DEAD -> 0xDE |
| 30 | + * |
| 31 | + * @param {Number} color |
| 32 | + * @return {Number} |
| 33 | + */ |
| 34 | +function green( color ) |
| 35 | +{ |
| 36 | + return ( color >> 8 ) & 0xFF; |
| 37 | +} |
| 38 | +
|
| 39 | +
|
| 40 | +/** |
| 41 | + * Extranct red color out of a color integer: |
| 42 | + * |
| 43 | + * 0x00DEAD -> 0xAD |
| 44 | + * |
| 45 | + * @param {Number} color |
| 46 | + * @return {Number} |
| 47 | + */ |
| 48 | +function blue( color ) |
| 49 | +{ |
| 50 | + return color & 0xFF; |
| 51 | +} |
| 52 | +
|
| 53 | +
|
| 54 | +/** |
| 55 | + * Converts an integer containing a color such as 0x00DEAD to a hex |
| 56 | + * string, such as '#00DEAD'; |
| 57 | + * |
| 58 | + * @param {Number} int |
| 59 | + * @return {String} |
| 60 | + */ |
| 61 | +function intToHex( int ) |
| 62 | +{ |
| 63 | + const mask = '#000000'; |
| 64 | +
|
| 65 | + const hex = int.toString( 16 ); |
| 66 | +
|
| 67 | + return mask.substring( 0, 7 - hex.length ) + hex; |
| 68 | +} |
| 69 | +
|
| 70 | +
|
| 71 | +/** |
| 72 | + * Converts a hex string containing a color such as '#00DEAD' to |
| 73 | + * an integer, such as 0x00DEAD; |
| 74 | + * |
| 75 | + * @param {Number} num |
| 76 | + * @return {String} |
| 77 | + */ |
| 78 | +function hexToInt( hex ) |
| 79 | +{ |
| 80 | + return parseInt( hex.substring( 1 ), 16 ); |
| 81 | +} |
| 82 | +
|
| 83 | +module.exports = { |
| 84 | + red, |
| 85 | + green, |
| 86 | + blue, |
| 87 | + intToHex, |
| 88 | + hexToInt, |
| 89 | +}; |
| 90 | +
|
| 91 | +"#; |
| 92 | + |
| 93 | +#[bench] |
| 94 | +fn parse_to_ast(b: &mut Bencher) { |
| 95 | + b.bytes = SOURCE.len() as u64; |
| 96 | + |
| 97 | + b.iter(|| { |
| 98 | + let ast = ratel::parser::parse(SOURCE.to_owned()).expect("Must parse"); |
| 99 | + |
| 100 | + ast |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +#[bench] |
| 106 | +fn parse_to_ast_and_transform_es5(b: &mut Bencher) { |
| 107 | + b.bytes = SOURCE.len() as u64; |
| 108 | + |
| 109 | + b.iter(|| { |
| 110 | + let mut ast = ratel::parser::parse(SOURCE.to_owned()).expect("Must parse"); |
| 111 | + |
| 112 | + let settings = ratel::transformer::Settings::target_es5(); |
| 113 | + |
| 114 | + ratel::transformer::transform(&mut ast, settings); |
| 115 | + |
| 116 | + ast |
| 117 | + }); |
| 118 | +} |
| 119 | + |
| 120 | +#[bench] |
| 121 | +fn codegen_from_ast(b: &mut Bencher) { |
| 122 | + let mut ast = ratel::parser::parse(SOURCE.to_owned()).expect("Must parse"); |
| 123 | + |
| 124 | + let settings = ratel::transformer::Settings::target_es5(); |
| 125 | + |
| 126 | + ratel::transformer::transform(&mut ast, settings); |
| 127 | + |
| 128 | + let output = ratel::codegen::generate_code(&ast, true); |
| 129 | + |
| 130 | + b.bytes = output.len() as u64; |
| 131 | + |
| 132 | + b.iter(|| { |
| 133 | + ratel::codegen::generate_code(&ast, true) |
| 134 | + }); |
| 135 | +} |
0 commit comments