I am currently dealing with a large JSON file that is 7MB in size, containing 11000 objects. Each object consists of various items structured like this:
[
{ ...
"startingTime":"Date1",
"endingTime":"Date2"
...
}
]
Within my HTML, there is a slider featuring daily Hours:Minutes.
As the user interacts with the slider, I need to update the DOM by filtering the data from the file to find:
StartingTime <= SelectedTimeOnSlider <= EndingTime
The filtered data is then utilized to display SVG shapes on a map (tubeMap + trains), ensuring that the previous shapes are removed from the DOM using remove()
before drawing new ones.
The issue arises when filtering all objects each time the user slides the cursor, causing the UI to become non-fluid.
Therefore, I experimented with an alternative approach: I employed a backend to generate a new JSON in the following format:
[
{
"00:00":{
"data":""
},
"00:01":{
"data":""
},
"00:02":{
"data":""
}
}
]
This new JSON is subsequently sent to the frontend. By having the backend perform the initial filtering calculations, when the user selects a specific daytime on the slider, I no longer need to filter through all objects again in real-time; instead, I select the ObjectKey corresponding to selectedTimeOnSlider
from the new format and add the data to the DOM.
However, I encountered delays while performing these computations upstream (taking more than 25 seconds from 00:00 to 02:00), which caused the frontend to wait excessively for the data.
Thus, I am seeking advice on how to enhance the performance of the filtering process or explore alternative approaches (such as indexing) to address this issue.
For context, the large JSON file is dynamic and varies based on the selected day on a calendar.
Update Below is the code snippet used for filtering the data:
let formatTrips = function (data:any) {
var arr = [], i, j;
for(i=0; i<24; i++) {
for(j=0; j<60; j++) {
arr.push((i<=9 ? "0"+i : i )+ ":" + (j<=9 ? "0"+j : j) );
}
}
var start = new Date().getTime();
var arrFormated = arr.map(time=> {
let schedulesFiltered=[];
let nowFormat = moment(time, 'HH:mm');
schedulesFiltered= data.filter(elm => {
let before = moment(elm.tp_org_r, 'HH:mm:ss'); //tp_org_r = startingTime
let after = moment(elm.tp_des_r, 'HH:mm:ss');//tp_des_r = endingTime
let checkifBetween = nowFormat.isBetween(before, after);
if (checkifBetween) {
return elm;
}
});
//console.log(schedulesFiltered)
return schedulesFiltered
})
var end = new Date().getTime();
var time = end - start;
console.log('Execution time: ' + time);
}
Additional information:
The HTTP request took 9 seconds to retrieve the file, but the main concern lies in the time-consuming filtering process involving a loop of 24*60 on an array of 11000+ object items.
In my initial approach, I load the JSON file once, saving it in a store (ngrx Angular store), and then apply filters for every change in slide value.