From e9db025250f73e982649d81e4c87bd6e82ff745a Mon Sep 17 00:00:00 2001 From: Giuseppe Date: Mon, 16 Jan 2017 20:44:17 +0100 Subject: [PATCH 1/4] Add benchmark for babel transform --- benchmark/babel.js | 30 +++++++++++++++++++++++++++++ benchmark/fixtures/babel.js | 38 +++++++++++++++++++++++++++++++++++++ gulpfile.babel.js | 11 ++++++++++- package.json | 2 ++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 benchmark/babel.js create mode 100644 benchmark/fixtures/babel.js diff --git a/benchmark/babel.js b/benchmark/babel.js new file mode 100644 index 00000000..fb33255d --- /dev/null +++ b/benchmark/babel.js @@ -0,0 +1,30 @@ +import path from 'path' +import Benchmark from 'benchmark' +import {transformFile} from 'babel-core' + +import plugin from '../src/babel' + +const transform = (file, opts = {}) => ( + new Promise((resolve, reject) => { + transformFile(path.resolve(__dirname, file), { + babelrc: false, + plugins: [plugin], + ...opts + }, (err, data) => { + if (err) { + return reject(err) + } + resolve(data) + }) + }) +) + +module.exports = new Benchmark({ + name: 'Babel transform', + minSamples: 500, + defer: true, + fn: async p => { + await transform('./fixtures/babel.js') + p.resolve() + } +}) diff --git a/benchmark/fixtures/babel.js b/benchmark/fixtures/babel.js new file mode 100644 index 00000000..ebc929eb --- /dev/null +++ b/benchmark/fixtures/babel.js @@ -0,0 +1,38 @@ +export const Test1 = () => ( +
+ test + test +

+

+

+

+

+

+ + +
+) + +export const Test2 = () => test + +export default class { + render() { + return ( +
+

test

+ + + +
+ ) + } +} diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 29b49b84..70179287 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -5,6 +5,7 @@ import gulp from 'gulp' import babel from 'gulp-babel' import {transformFile} from 'babel-core' import size from 'human-size' +import benchmark from 'gulp-benchmark' gulp.task('transpile', () => { gulp.src('src/**/*.js') @@ -41,5 +42,13 @@ gulp.task('runtime-size', async () => { } }) -gulp.task('watch', () => gulp.watch('src/*', ['transpile'])) +gulp.task('benchmark', () => { + gulp.src('*.js', {read: false, cwd: './benchmark'}) + .pipe(babel()) + .pipe(benchmark()) +}) +gulp.task('watch', () => { + gulp.watch('src/*', ['transpile']) + gulp.watch('benchmark/*.js', ['benchmark']) +}) gulp.task('default', ['transpile', 'watch']) diff --git a/package.json b/package.json index f61c65bf..ca6492ab 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,10 @@ "babel-preset-react": "^6.16.0", "babel-preset-stage-3": "^6.16.0", "babel-register": "^6.18.0", + "benchmark": "^2.1.3", "gulp": "^3.9.1", "gulp-babel": "^6.1.2", + "gulp-benchmark": "^1.1.1", "human-size": "^1.1.0", "mz": "^2.6.0", "react": "^15.4.1", From 4e04c8b05c3787a4523f956f0f0e82abaeac37f1 Mon Sep 17 00:00:00 2001 From: Giuseppe Date: Tue, 17 Jan 2017 19:51:41 +0100 Subject: [PATCH 2/4] Avoid doing I/O in the benchmark fn --- benchmark/babel.js | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/benchmark/babel.js b/benchmark/babel.js index fb33255d..e05d7c6a 100644 --- a/benchmark/babel.js +++ b/benchmark/babel.js @@ -1,30 +1,20 @@ -import path from 'path' +import {resolve} from 'path' import Benchmark from 'benchmark' -import {transformFile} from 'babel-core' +import {transform as babel} from 'babel-core' +import fs from 'fs' import plugin from '../src/babel' -const transform = (file, opts = {}) => ( - new Promise((resolve, reject) => { - transformFile(path.resolve(__dirname, file), { - babelrc: false, - plugins: [plugin], - ...opts - }, (err, data) => { - if (err) { - return reject(err) - } - resolve(data) - }) - }) -) +const read = path => fs.readFileSync(resolve(__dirname, path), 'utf8') +const fixture = read('./fixtures/babel.js') module.exports = new Benchmark({ name: 'Babel transform', minSamples: 500, - defer: true, - fn: async p => { - await transform('./fixtures/babel.js') - p.resolve() + fn: () => { + babel(fixture, { + babelrc: false, + plugins: [plugin] + }) } }) From 66d23052c09859ede3d0f89cf3a7823b0b924715 Mon Sep 17 00:00:00 2001 From: Giuseppe Date: Tue, 17 Jan 2017 19:56:41 +0100 Subject: [PATCH 3/4] nit --- benchmark/babel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/babel.js b/benchmark/babel.js index e05d7c6a..208a8c06 100644 --- a/benchmark/babel.js +++ b/benchmark/babel.js @@ -1,11 +1,11 @@ import {resolve} from 'path' import Benchmark from 'benchmark' import {transform as babel} from 'babel-core' -import fs from 'fs' +import {readFileSync} from 'fs' import plugin from '../src/babel' -const read = path => fs.readFileSync(resolve(__dirname, path), 'utf8') +const read = path => readFileSync(resolve(__dirname, path), 'utf8') const fixture = read('./fixtures/babel.js') module.exports = new Benchmark({ From e455128e741b9d83b89a9e32fe30957d4730d249 Mon Sep 17 00:00:00 2001 From: Giuseppe Date: Tue, 17 Jan 2017 19:57:41 +0100 Subject: [PATCH 4/4] reorder imports --- benchmark/babel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/babel.js b/benchmark/babel.js index 208a8c06..b5d1694a 100644 --- a/benchmark/babel.js +++ b/benchmark/babel.js @@ -1,7 +1,7 @@ +import {readFileSync} from 'fs' import {resolve} from 'path' import Benchmark from 'benchmark' import {transform as babel} from 'babel-core' -import {readFileSync} from 'fs' import plugin from '../src/babel'