Skip to content

Commit

Permalink
Fix a perf regression around handling too-large numbers.
Browse files Browse the repository at this point in the history
Fixes #130.
  • Loading branch information
bhollis committed Nov 25, 2016
1 parent 6f29fba commit a92f048
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ JSONView 1.2.0
---
* Add a preference to use the built-in Firefox JSON viewer.
* Tell Firefox we support multi-process mode.
* Fix a performance regression that could severely slow down rendering large documents.

JSONView 1.1.1
---
Expand Down
59 changes: 22 additions & 37 deletions lib/jsonview.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,50 +93,35 @@ var JSONView = Class({
* string with !isNaN() for number-ness, and render it with number styling, sans-quotes.
*/
safeStringEncodeNums: function(jsonString) {
function betweenOrderedPairs(numberIndex) {
function betweenPair(pair, pairIndex, o) {
if (numberIndex < pair[0]) {
return false;
}
if (numberIndex <= pair[1]) {
return true;
}
if (pairIndex + 1 === o.length) {
return false;
}
return false;
}
return orderedPairs.some(betweenPair);
}
var viewString = jsonString.replace(/\u200B/g, "\u200B\u200B");

// Find unescaped quotes by searching for a non-backslash followed by 0 or even
// pairs of backslashes, then a quote
var quoteFinder = /[^\\](\\\\)*(?=")/g;

// JSON legal number matcher, Andrew Cheong, http://stackoverflow.com/questions/13340717
var numberFinder = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/g;
var quoteMap = [];
var orderedPairs = [];
var viewString = jsonString.replace(/\u200B/g, "\u200B\u200B");
var match;

while ((match = quoteFinder.exec(viewString)) !== null) {
quoteMap.push(match.index + match[0].length);
}

// If we don't have an even number of quotes, bail and parse viewString as-is.
if (quoteMap.length % 2 !== 0) {
return viewString;
var quoteFinder = /([^\\]|^)(\\\\)*(?=")/gm;

// This has some memory of what its last state was
var wasInQuotes = false;
function isInsideQuotes(str) {
var inQuotes = (str.match(quoteFinder) || []).length % 2 === 1;
if (wasInQuotes) {
inQuotes = !inQuotes;
}
wasInQuotes = inQuotes;
return inQuotes;
}

while (quoteMap.length) {
orderedPairs.push(quoteMap.splice(0, 2));
var startIndex = 0;
function replaceNumbers(match, index) {
// Substring should be copy-on-write, and thus cheap
var lookback = viewString.substring(startIndex, index);
var insideQuotes = isInsideQuotes(lookback);
startIndex = index + match.length;
return insideQuotes ? match : '"\u200B' + match + '"';
}

return viewString.replace(numberFinder,
(match, ndx) =>
(betweenOrderedPairs(ndx) ? match : '"\u200B' + match + '"')
);
// JSON legal number matcher, Andrew Cheong, http://stackoverflow.com/questions/13340717
var numberFinder = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/g;
return viewString.replace(numberFinder, replaceNumbers);
},

// nsIRequestObserver::onStopRequest
Expand Down

0 comments on commit a92f048

Please sign in to comment.