From fd08950d601ddf8f0fe929fcf47b7b51194f7136 Mon Sep 17 00:00:00 2001 From: Matt Hemsteger Date: Sat, 4 Jul 2020 10:38:47 -0700 Subject: [PATCH] Add a memoize to makeLineColumnIndex for large input scenarios --- src/parsimmon.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/parsimmon.js b/src/parsimmon.js index 66b4456..cc0d766 100644 --- a/src/parsimmon.js +++ b/src/parsimmon.js @@ -355,6 +355,8 @@ function mergeReplies(result, last) { }; } +// Hold a simple memoize for the last value +var lastLineColumnIndex = {}; function makeLineColumnIndex(input, i) { if (isBuffer(input)) { return { @@ -363,16 +365,25 @@ function makeLineColumnIndex(input, i) { column: -1 }; } + // if we are calling this function with the same arguments as last time + // return the memoized value to prevent expensive processing below + if (lastLineColumnIndex.input === input && lastLineColumnIndex.i === i) { + return lastLineColumnIndex.value; + } var lines = input.slice(0, i).split("\n"); // Note that unlike the character offset, the line and column offsets are // 1-based. var lineWeAreUpTo = lines.length; var columnWeAreUpTo = lines[lines.length - 1].length + 1; - return { + var value = { offset: i, line: lineWeAreUpTo, column: columnWeAreUpTo }; + lastLineColumnIndex.input = input; + lastLineColumnIndex.i = i; + lastLineColumnIndex.value = value; + return value; } // Returns the sorted set union of two arrays of strings