Skip to content

Commit e0425a4

Browse files
mmynkJustin Martin
authored andcommitted
Fix performance issues (#42)
* Added json-source-map for faster parsing of JSONs * Added reference to json-source-map script * Changed getStartAndEndPosOfDiff logic
1 parent bfe400d commit e0425a4

File tree

3 files changed

+425
-96
lines changed

3 files changed

+425
-96
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ <h2 class="left">Online JSON Compare</h2>
7878
<script src="www/lib/codemirror/lib/util/formatting.js" charset="utf-8"></script>
7979
<script src="www/lib/codemirror/mode/javascript/javascript.js" charset="utf-8"></script>
8080
<script src="www/lib/codemirror/addon/edit/matchbrackets.js" charset="utf-8"></script>
81+
<script src="www/lib/json-source-map.js" charset="utf-8"></script>
8182
<script src="js/main.js" charset="utf-8"></script>
8283
<script>
8384
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){

js/main.js

Lines changed: 14 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -86,106 +86,24 @@
8686
});
8787
}
8888

89-
function getStartAndEndPosOfDiff(textValue, diff) {
90-
var findPath = diff.path;
91-
var contexts = {
92-
ARRAY: 'ARRAY',
93-
OBJECT: 'OBJECT'
94-
};
95-
var QUOTE = '"';
96-
var OBJ_OPEN = '{';
97-
var OBJ_CLOSE = '}';
98-
var ARR_OPEN = '[';
99-
var ARR_CLOSE = ']';
100-
var SEPARATOR = ',';
101-
var ESCAPE = '\\';
102-
var NL = '\n';
103-
var OBJ_PROPERTY_RGX = /^"([^"]|\\")*"(?=\s*:)/g;
104-
var startPos, endPos, currChar, prevChar, currPath = [], contextStack = [], line = 0, ch = 0, inString = false;
105-
for (var i = 0; i < textValue.length; i++) {
106-
ch++;
107-
currChar = textValue[i];
108-
if (currChar === NL) {
109-
line++;
110-
ch = 0;
111-
} else if (currChar === OBJ_OPEN) {
112-
currPath.push(null);
113-
contextStack.push(contexts.OBJECT);
114-
} else if (currChar === ARR_OPEN) {
115-
currPath.push(0);
116-
contextStack.push(contexts.ARRAY);
117-
} else if (currChar === QUOTE && !inString && prevChar !== ESCAPE) {
118-
inString = true;
119-
var prop = getNextObjProperty(i);
120-
if (prop) {
121-
currPath.push(prop);
122-
}
123-
} else if (currChar === SEPARATOR && !inString) {
124-
if (context() === contexts.ARRAY) {
125-
var currArrayIdx = currPath[currPath.length - 1];
126-
currArrayIdx = typeof(currArrayIdx ) === 'number' ? currArrayIdx + 1 : 0;
127-
currPath.pop();
128-
currPath.push(currArrayIdx);
129-
} else {
130-
currPath.pop();
131-
}
132-
} else if (currChar === QUOTE && inString) {
133-
inString = false;
134-
} else if (currChar === OBJ_CLOSE) {
135-
contextStack.pop();
136-
currPath.pop();
137-
// look behind for empty object
138-
var matches = textValue.split('').reverse().join('').substr(textValue.length - i).match(/^\s*{/g) || [];
139-
var isEmptyObject = matches.length > 0;
140-
if (!isEmptyObject) {
141-
currPath.pop();
142-
}
143-
} else if (currChar === ARR_CLOSE) {
144-
contextStack.pop();
145-
currPath.pop();
146-
}
147-
148-
var currPathStr = '/' + currPath.filter(function (item) {
149-
return item !== null;
150-
}).join('/');
151-
if (currPathStr === findPath && !startPos) {
152-
startPos = {
153-
line: line,
154-
ch: ch - 1
89+
function getStartAndEndPosOfDiff(textValue, diff) {
90+
var result = parse(textValue);
91+
var pointers = result.pointers;
92+
var key = diff.path;
93+
var start = {
94+
line: pointers[key].key.line,
95+
ch: pointers[key].key.column
15596
};
156-
} else if (currPathStr.indexOf(findPath) === 0 && !(/\s/g).test(currChar)) {
157-
endPos = {
158-
line: line,
159-
ch: ch
97+
var end = {
98+
line: pointers[key].valueEnd.line,
99+
ch: pointers[key].valueEnd.column
160100
};
161-
}
162101

163-
prevChar = currChar;
164-
}
165-
166-
function getNextObjProperty(idx) {
167-
var matches = textValue.substr(idx).match(OBJ_PROPERTY_RGX) || [];
168-
var next = matches[0];
169-
if (next) {
170-
next = next.substr(1, next.length - 2);
171-
}
172-
return next;
173-
}
174-
175-
function followedByComma(idx) {
176-
var matches = textValue.substr(idx + 1).match(/^\s*,/g) || [];
177-
return matches.length > 0;
178-
}
179-
180-
function context() {
181-
return contextStack[contextStack.length - 1];
182-
}
183-
184-
return {
185-
start: startPos,
186-
end: endPos
102+
return {
103+
start: start,
104+
end: end
105+
}
187106
}
188-
}
189107

190108
function indexToPos(textValue, i) {
191109
var beginStr = textValue.substr(0, i);

0 commit comments

Comments
 (0)