[JavaScript] JSON parsing faster alternative
Posted: Wed Sep 25, 2019 11:58 am
My esp32 chip is used for recording some important data into local memory. I need to send these data to the user webbrowser for drawing a chart. It is about 1MB per hour and I need charts with up to 5days of data (up to 120MB of raw data).
At the moment I have written an http server and JSON parser. When user requests data, raw data from memory are read and parsed into JSON for sanding. Parsing raw data into text json result in extending each 1MB (of raw) into about 2MB (of text).
Local browser waits for JSON file and using JS JSON.parse(this.responseText) parses it into object for adding into an javascript array.
JSON.parse tooks lots of time! About 5 sec for each 2MB (1MB of raw).
I am wandering what can I do for speeding up my code both in terms of used memory and execution time.
At the moment I have written an http server and JSON parser. When user requests data, raw data from memory are read and parsed into JSON for sanding. Parsing raw data into text json result in extending each 1MB (of raw) into about 2MB (of text).
Local browser waits for JSON file and using JS JSON.parse(this.responseText) parses it into object for adding into an javascript array.
JSON.parse tooks lots of time! About 5 sec for each 2MB (1MB of raw).
Code: Select all
function loadChartData(address) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = loadChartDataReady;
xhttp.open("GET", "http://192.168.1.1/"+address, true);
xhttp.send();
}
function loadChartDataReady() {
if (this.readyState != 4) return;
if (this.status != 200) { return; }
try {
var json = JSON.parse(this.responseText);
for(var i = 1; i < json.length; i++) {
var obj = json[i];
series.splice(i - 1, 0, {ts: obj[0], time: obj[1], s: obj[2], m: obj[3], on: obj[4]} );
}
updateChart();
} catch(err) {
console.log("error: " + err);
}
}
- Can I parse raw data somehow in javascript?
- Can I change JSON.parse into some another function for speeding up?
- Can I change XMLHttpRequest into something another for start parsing data while they are still being sent?