Skip to content

Fix performance issues #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ <h2 class="left">Online JSON Compare</h2>
<script src="www/lib/codemirror/lib/util/formatting.js" charset="utf-8"></script>
<script src="www/lib/codemirror/mode/javascript/javascript.js" charset="utf-8"></script>
<script src="www/lib/codemirror/addon/edit/matchbrackets.js" charset="utf-8"></script>
<script src="www/lib/json-source-map.js" charset="utf-8"></script>
<script src="js/main.js" charset="utf-8"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
Expand Down
110 changes: 14 additions & 96 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,106 +86,24 @@
});
}

function getStartAndEndPosOfDiff(textValue, diff) {
var findPath = diff.path;
var contexts = {
ARRAY: 'ARRAY',
OBJECT: 'OBJECT'
};
var QUOTE = '"';
var OBJ_OPEN = '{';
var OBJ_CLOSE = '}';
var ARR_OPEN = '[';
var ARR_CLOSE = ']';
var SEPARATOR = ',';
var ESCAPE = '\\';
var NL = '\n';
var OBJ_PROPERTY_RGX = /^"([^"]|\\")*"(?=\s*:)/g;
var startPos, endPos, currChar, prevChar, currPath = [], contextStack = [], line = 0, ch = 0, inString = false;
for (var i = 0; i < textValue.length; i++) {
ch++;
currChar = textValue[i];
if (currChar === NL) {
line++;
ch = 0;
} else if (currChar === OBJ_OPEN) {
currPath.push(null);
contextStack.push(contexts.OBJECT);
} else if (currChar === ARR_OPEN) {
currPath.push(0);
contextStack.push(contexts.ARRAY);
} else if (currChar === QUOTE && !inString && prevChar !== ESCAPE) {
inString = true;
var prop = getNextObjProperty(i);
if (prop) {
currPath.push(prop);
}
} else if (currChar === SEPARATOR && !inString) {
if (context() === contexts.ARRAY) {
var currArrayIdx = currPath[currPath.length - 1];
currArrayIdx = typeof(currArrayIdx ) === 'number' ? currArrayIdx + 1 : 0;
currPath.pop();
currPath.push(currArrayIdx);
} else {
currPath.pop();
}
} else if (currChar === QUOTE && inString) {
inString = false;
} else if (currChar === OBJ_CLOSE) {
contextStack.pop();
currPath.pop();
// look behind for empty object
var matches = textValue.split('').reverse().join('').substr(textValue.length - i).match(/^\s*{/g) || [];
var isEmptyObject = matches.length > 0;
if (!isEmptyObject) {
currPath.pop();
}
} else if (currChar === ARR_CLOSE) {
contextStack.pop();
currPath.pop();
}

var currPathStr = '/' + currPath.filter(function (item) {
return item !== null;
}).join('/');
if (currPathStr === findPath && !startPos) {
startPos = {
line: line,
ch: ch - 1
function getStartAndEndPosOfDiff(textValue, diff) {
var result = parse(textValue);
var pointers = result.pointers;
var key = diff.path;
var start = {
line: pointers[key].key.line,
ch: pointers[key].key.column
};
} else if (currPathStr.indexOf(findPath) === 0 && !(/\s/g).test(currChar)) {
endPos = {
line: line,
ch: ch
var end = {
line: pointers[key].valueEnd.line,
ch: pointers[key].valueEnd.column
};
}

prevChar = currChar;
}

function getNextObjProperty(idx) {
var matches = textValue.substr(idx).match(OBJ_PROPERTY_RGX) || [];
var next = matches[0];
if (next) {
next = next.substr(1, next.length - 2);
}
return next;
}

function followedByComma(idx) {
var matches = textValue.substr(idx + 1).match(/^\s*,/g) || [];
return matches.length > 0;
}

function context() {
return contextStack[contextStack.length - 1];
}

return {
start: startPos,
end: endPos
return {
start: start,
end: end
}
}
}

function indexToPos(textValue, i) {
var beginStr = textValue.substr(0, i);
Expand Down
Loading