I am experiencing performance issues with JSONPath implemented in JavaScript using the Stephan Goessner Library.
Below is an example of the JSON structure causing the problem:
[
{
id:1,
name: "lorem",
elements:
[
{
id:101,
date: Fri Jan 31 2014 18:03:41 GMT+0100 (Paris, Madrid), // Javascript Date Object,
instances: [
{
id: 1001,
properties: [
{
code: 'ipsum lens',
id: 1001,
values: [
"value1",
"value2",
...
// less than 5 values
]
},
... , // between 20 to 40 items
]
},
... // About 100 items
]
},
... // Less than 5 items
],
meta: [
{
id: 10000,
code: 'codeabc',
property1: true,
property2: false,
property3: 2939,
},
... // between 20 to 40 items
]
},
... // 4 items
]
The data quantities are as follows:
- level1 contains 4 items
- level2 'elements' contains 2 items on each level
- level3 'instances' contains about 100 items per specific element
- level4 'properties' contains about 15 items
When using JSONPath, the following code results in a response time of approximately 4 seconds in Chrome:
var startDate = new Date();
var searchedValue = jsonPath(
myJSONStore,
"$.[?(@.name == 'myelementname')].elements[?(@.id == 100)].date"
)[0];
console.log(date)
console.log("Retrieved in " + ((new Date().getTime() - startDate.getTime()) / 1000) + "s")
I found this delay too long considering the amount of data present. I also tried the traditional method without JSONPath:
var startDate = new Date();
var searchedValue = null;
for(i in myJSONStore) {
if (myJSONStore[i].name == 'myelementname') {
for (j in myJSONStore[i].elements) {
if (myJSONStore[i].elements[j].id == 100) {
var date = myJSONStore[i].elements[j].date;
break;
}
}
}
}
console.log(date)
console.log("Retrieved in " + ((new Date().getTime() - startDate.getTime()) / 1000) + "s")
This time, it only took 1 millisecond.
While JSONPath is appealing due to its simplicity and concise syntax (following the kiss principle), the significant increase in calculation delay of about 4000 times is concerning. Is there a way to reduce this delay?