Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser performance is very bad when typescript is enabled #9972

matthewrobertson opened this issue May 13, 2019 · 1 comment


None yet
3 participants
Copy link

commented May 13, 2019

Bug Report

Current Behavior

I reported this before in #8779, and we made things a bit better, but it seems to have regressed again recently. The problem is the typescript parser plugin uses exceptions to back out of lookahead branches. For example, see the tsTryParseAndCatch method:

tsTryParseAndCatch<T>(f: () => T): ?T {
const state = this.state.clone();
try {
return f();
} catch (e) {
if (e instanceof SyntaxError) {
this.state = state;
return undefined;
throw e;

This causes the typescript plugin to degrade the performance of the parser in a manner that is non-linear WRT the input size.

Consider the following script to demo the problem:

const {parse} = require('@babel/parser');
const fs = require('fs');

const input = fs.readFileSync(process.argv[2], 'utf8');

let start =;
parse(input, {
  plugins: [],
console.log('parse time with no TS ' + ( - start));

start =;
parse(input, {
  plugins: ['typescript'],
console.log('parse time with TS ' + ( - start));

The perf delta gets progressively bigger as the input size increases:

C:\Perf> ls .\samples\

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        5/13/2019  11:50 AM        4364428 long.js
-a----        5/13/2019  11:45 AM        1454808 medium.js
-a----        5/13/2019  11:45 AM         290960 short.js

C:\Perf> npm run parser_perf .\samples\short.js
parse time with no TS 162
parse time with TS 221
C:\Perf> npm run parser_perf .\samples\medium.js
parse time with no TS 413
parse time with TS 1070
C:\Perf> npm run parser_perf .\samples\long.js
parse time with no TS 771
parse time with TS 6617


  • Babel version(s): @babel/parser v7.4.4
  • Node/npm version: Node v10.15.3

Possible Solution
We need to avoid using exceptions for control flow. Ideally, we would do some refactoring to completely remove the tsTryParseAndCatch from the typescript plugin. In the short term, adding a few conditionals to limit its use will probably be sufficient.


This comment has been minimized.

Copy link

commented May 13, 2019

Hey @matthewrobertson! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
that typically always has someone willing to help. You can sign-up here
for an invite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.