From 4e2072def883ea3d52c07f3105e963226cd7da3a Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sat, 2 Jul 2016 17:06:06 +0200 Subject: [PATCH] Fix performance regression introduced in 6.8.2 This commit e6c11a0 (#19) made a big performance regression. The reason was that parseConditional was always cloning the current state even if no question mark (potential conditional or flow-optional token) was at the current position. Simply checking if questionmark matches the current token solves the problem. Fixes #62 --- src/plugins/flow.js | 26 ++++++++++++------- .../flow/optional-type/2/options.json | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/plugins/flow.js b/src/plugins/flow.js index bd0426b0b5..7c85da6255 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -732,18 +732,24 @@ export default function (instance) { instance.extend("parseConditional", function (inner) { return function (expr, noIn, startPos, startLoc, refNeedsArrowPos) { - const state = this.state.clone(); - try { - return inner.call(this, expr, noIn, startPos, startLoc); - } catch (err) { - if (refNeedsArrowPos && err instanceof SyntaxError) { - this.state = state; - refNeedsArrowPos.start = this.state.start; - return expr; - } else { - throw err; + // only do the expensive clone if there is a question mark + // and if we come from inside parens + if (refNeedsArrowPos && this.match(tt.question)) { + const state = this.state.clone(); + try { + return inner.call(this, expr, noIn, startPos, startLoc); + } catch (err) { + if (err instanceof SyntaxError) { + this.state = state; + refNeedsArrowPos.start = err.pos || this.state.start; + return expr; + } else { + throw err; + } } } + + return inner.call(this, expr, noIn, startPos, startLoc); }; }); diff --git a/test/fixtures/flow/optional-type/2/options.json b/test/fixtures/flow/optional-type/2/options.json index 9e093bfdcd..e247a786c1 100644 --- a/test/fixtures/flow/optional-type/2/options.json +++ b/test/fixtures/flow/optional-type/2/options.json @@ -1,3 +1,3 @@ { - "throws": "Unexpected token (1:12)" + "throws": "Unexpected token (1:13)" }