From bf9183269b89cf55c2d2464fc6354a139571f54e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 10 Sep 2026 15:12:51 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20[performance=20improvement]=20optim?= =?UTF-8?q?ize=20token=20scanning=20in=20tryParseGoalSeek?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced dual array scans (`Array.prototype.some` and `Array.prototype.findIndex`) in `tryParseGoalSeek` with a single `for` loop that implements early bail-out logic once both the assignment operator and the unknown variable are found. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- src/engine/parser.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/engine/parser.ts b/src/engine/parser.ts index ace0915..66a0ed6 100644 --- a/src/engine/parser.ts +++ b/src/engine/parser.ts @@ -98,15 +98,25 @@ function filterNoiseTokens(toks: Token[], knownVars: Set): Token[] { function tryParseGoalSeek(filtered: Token[], knownVars: Set): Node | null { // Goal seek: `? * 1.2 = 1000` → find x where lhs(x) = rhs(x) - const hasUnknown = filtered.some((tk) => tk.t === "unknown"); - if (hasUnknown) { - const assignIdx = filtered.findIndex((tk) => tk.t === "assign"); - if (assignIdx >= 0) { - const lhs = new Parser(filtered.slice(0, assignIdx), knownVars).parseSeq(); - const rhs = new Parser(filtered.slice(assignIdx + 1), knownVars).parseSeq(); - if (lhs && rhs) return { k: "goalseek", lhs, rhs }; + let hasUnknown = false; + let assignIdx = -1; + const len = filtered.length; + for (let i = 0; i < len; i++) { + const t = filtered[i].t; + if (t === "unknown") { + hasUnknown = true; + if (assignIdx !== -1) break; + } else if (t === "assign" && assignIdx === -1) { + assignIdx = i; + if (hasUnknown) break; } } + + if (hasUnknown && assignIdx >= 0) { + const lhs = new Parser(filtered.slice(0, assignIdx), knownVars).parseSeq(); + const rhs = new Parser(filtered.slice(assignIdx + 1), knownVars).parseSeq(); + if (lhs && rhs) return { k: "goalseek", lhs, rhs }; + } return null; }