lossless-json icon indicating copy to clipboard operation
lossless-json copied to clipboard

Question about performance compared to JSON.parse

Open MaevskiyE opened this issue 1 year ago • 3 comments

Hi community

I have a question and unfortunately didn't find other place to ask.

So, could someone explain, why native JSON.parse or Response: json() method are working so slower than parse method of lossless-json?

I created example to compare. First of all you need to take big JSON. I used about 100Mb file. You can use this one if you want Then play here https://codepen.io/MaevskiyE/pen/vYwYPEq and compare parsing time

MaevskiyE avatar May 10 '24 16:05 MaevskiyE

The lossless-json parser is actually slower than the native, built-in JSON.parse, and that is also what I would expect.

In your benchmark it will be easier to compare duration when you use console.time and console.timeEnd BTW :).

josdejong avatar May 11 '24 07:05 josdejong

@josdejong , thank you for response. In most cases maybe lossless-json parser slower that native JSON.parse. But in my case with a lot of numbers - it's significantly faster. 2-3sec vs 21sec. As I understood it's because lossless-json parser even doesn't try to convert value to actual number and leave it as string. Can you confirm my assumption? Maybe you can suggest a way, how to make the same with JSON.parse using it's second argument reviver? Thank you

MaevskiyE avatar May 11 '24 12:05 MaevskiyE

You have to use console.time and console.timeEnd like:

console.time('parse-my-thing')

// ... do the work

console.timeEnd('parse-my-thing')

I've run your example test.json file. In Firefox I get:

// Firefox, parsing 100 MB test.json file
JSON.parse: 443ms - timer ended 
lossless-json: 1471ms - timer ended

Which is what I expect and have seen all the time, lossless-json being 2-5 fives slower than the built-in JSON.parse.

On Chrome however I see the issue you encounter:

// Chrome, parsing 100 MB test.json file
JSON.parse: 7042.577880859375 ms
lossless-json: 978.43701171875 ms

I've tested again with other JSON files (like example from here), I get the same, expected behavior of Chrome and Firefox both having the native implementation being faster than lossless-json (as expected). So it looks like your very JSON file has something specific that triggers a bad performance in Chrome. When I change the longitude and latitude properties from strings into numbers, the performance issue of Chrome is gone and it is faster than lossless-json (as expected).

josdejong avatar May 11 '24 18:05 josdejong

@josdejong , I very appreciate your help. This really helped! You saved my life :)

p.s. now it's interesting why Chrome has problems with numbers like String and Firefox doesn't

MaevskiyE avatar May 23 '24 07:05 MaevskiyE