From 46f8f79489102c558747d66e26ca094abaa1abb5 Mon Sep 17 00:00:00 2001 From: Danielle Church Date: Sat, 8 Sep 2018 20:59:50 -0400 Subject: [PATCH] Fix performance issue in Wikifier.parse The Wikifier.textPrimitives.unquoted method of parsing quoted strings is very slow when dealing with long strings that look like variable names, since it has to test each initial substring to see if it qualifies as "unquoted". This fixes the problem by removing all quoted strings from the input before parsing and restoring them after all of the calls to alter(). --- targets/engine.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/targets/engine.js b/targets/engine.js index 3e536aa..245bace 100644 --- a/targets/engine.js +++ b/targets/engine.js @@ -1488,14 +1488,18 @@ Wikifier.prototype.fullArgs = function (includeName) { Wikifier.parse = function (input) { var m, re, b = input, found = [], - g = Wikifier.textPrimitives.unquoted; + qstrings = [], qreg = /'[^'\\]*(?:\\.[^'\\]*)*'|"[^"\\]*(?:\\.[^"\\]*)*"/g, + preg = /\x00QSTR.(\d+)\x00/g;; + + // Extract all the quoted strings and save them for later + b = b.replace(qreg, function(qstr) {qstrings.push(qstr); return "\x00QSTR."+(qstrings.length-1)+"\x00"}); function alter(from,to) { - b = b.replace(new RegExp(from+g,"gim"),to); + b = b.replace(new RegExp(from,"gim"),to); return alter; } // Extract all the variables, and set them to 0 if undefined. - re = new RegExp(Wikifier.textPrimitives.variable+g,"gi"); + re = new RegExp(Wikifier.textPrimitives.variable,"gi"); while (m = re.exec(input)) { if (!~found.indexOf(m[0])) { // This deliberately contains a 'null or undefined' check @@ -1517,6 +1521,9 @@ Wikifier.parse = function (input) { // New operators ("\\bis\\b", " == ") ("\\bto\\b", " = "); + + // Restore all the quoted strings to their place + b = b.replace(preg, function(pstr, qidx) {return qstrings[qidx];}); return b }; Wikifier.formatHelpers = {