Answer the question
In order to leave comments, you need to log in
How can I speed up json parsing?
I have a price.json file (897 KB) that is steaming:
var values = [11,694];
var t0 = performance.now();
var myInit = {
method: 'GET',
headers: {
'Content-Type':'application/json'
},
mode: 'cors',
cache: 'default'
};
let myRequest = new Request("price.json",myInit);
(async () => { fetch(myRequest)
.then(function(resp){
return resp.json();
})
.then(function(data){
console.log(data);
for (let i = 0; i < document.getElementsByName('js-price').length; i++) {
document.getElementsByName('js-price')[i].innerHTML = "$" + Number(data[values[i]]['lastPrice']).toFixed(2); //694
document.getElementsByName('js-price-percent')[i].innerHTML = Number(data[values[i]]['priceChangePercent']).toFixed(2); //694
}
console.log(`Время закгрузки JSON ${(performance.now() - t0).toFixed(2)}`);
})
})();
Answer the question
In order to leave comments, you need to log in
897kb is
almost a megabyte to download! I don’t believe that this happens with your Internet in 80 milliseconds, rather, everything from the cache is taken
for json parsing, it just takes a few tens of milliseconds, and it’s unrealistically fast for a megabyte
ps file in c ++ there is a simdjson parser, it parses ten times faster, but it does this not in memory, but by tokens, right in the process of analyzing the result, there is something like, get the next token, look at its type, name and either skip (including objects) or recursively work with the object
the browser does not know how, in theory, you can try to write such a parser in javascript, but I don’t believe that it will be faster
upd: https://www.npmjs.com/package/simdjson
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question