Skip to content

Instantly share code, notes, and snippets.

@TheLarkInn
Created November 11, 2017 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheLarkInn/8b1c5f275b3ae1c604abab78c12606ef to your computer and use it in GitHub Desktop.
Save TheLarkInn/8b1c5f275b3ae1c604abab78c12606ef to your computer and use it in GitHub Desktop.
benchparse.js
const fs = require('fs');
const path = require('path');
const Benchmark = require('benchmark');
const suite = new Benchmark.Suite;
const ts = require('typescript');
const acorn = require('acorn');
const babylon = require('babylon');
const parseWithTypeScript = (sourcefile) => ts.createSourceFile('parse-me.js', sourcefile, ts.ScriptTarget.Latest, false, ts.ScriptKind.JS);
const parseWithAcorn = (sourcefile) => acorn.parse(sourcefile, {sourceType: 'module'});
const parseWithBabylon = (sourcefile) => babylon.parse(sourcefile, { sourceType: 'module' });
const scriptSource = fs.readFileSync(path.join(__dirname, 'node_modules', 'typescript', 'lib', 'typescript.js')).toString();
suite
.add('acorn parse', () => parseWithAcorn(scriptSource))
.add('typescript parse', () => parseWithTypeScript(scriptSource))
.add('babylon parse', () => parseWithBabylon(scriptSource))
.on('cycle', (event) => {console.log(String(event.target))})
.on('complete', function (){ console.log('Fastest is ' + this.filter('fastest').map('name')) })
.run({'asnyc': true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment