Go Against JavaScript JSON Parsing

Recently, I needed to parse the JSON that the Chrome web browser creates when recording events in its dev tools, and get some synchronization data from it. Chrome can produce quite a lot of data in a short amount of time, so the Ruby analyzer you originally created was pretty slow.

Since I am learning Go, I decided to write scripts in both Go and JavaScript / Node and compare them.

The simplest form of a JSON file is what I have in this Gist . It contains an event representing the request sent to retrieve the page, and an event representing the response. As a rule, there is a huge amount of additional data for sifting. This is his own problem, but not what I'm worried about in this matter.

The JavaScript script I wrote is here , and the Go program I wrote is. This is the first useful thing I wrote on Go, so I’m sure all this is bad. However, I noticed that this is much slower than JavaScript when parsing a large JSON file.

Time with a 119 MB JSON file in Go:

$ time ./parse data.json = 22 Requests Min Time: 0.77 Max Time: 0.77 Average Time: 0.77 ./gm data.json 4.54s user 0.16s system 99% cpu 4.705 total 

Time with 119 megabyte JSON file in JavaScript / Node:

 $ time node parse.js data.json = 22 Requests Min Time: 0.77 Max Time: 0.77 Avg Time: 0.77 node jm.js data.json 1.73s user 0.24s system 100% cpu 1.959 total 

(In this example, the min / max / average values ​​are identical because I duplicated JSON objects to have a very large dataset, but that doesn't matter.)

I'm curious if JavaScript / Node just works faster when parsing JSON (which is probably not surprising), or if something that I am doing is completely wrong in the Go program, I am also just curious about what I am doing wrong in the Go program in general because I'm sure this is not the case.

Note that while these two scripts do more than parse, it is definitely json.Unmarshal() in Go, which adds a lot of time to the program.

Update

I added a Ruby script :

 $ ruby parse.rb = 22 Requests Min Time: 0.77 Max Time: 0.77 Avg Time: 0.77 ruby parse.rb 4.82s user 0.82s system 99% cpu 5.658 total 
+6
source share
1 answer

With Go, you parse JSON into statically typed structures. With JS and Ruby, you parse it into hash tables.

In order to parse JSON into the structures you defined, the json package needs to know the names and types of their fields. To do this, it uses a reflection package, which is much slower than direct access to these fields.

Depending on what you do with the data after analyzing it, the extra parsing time may pay for itself. Go data structures use less memory than hash tables, and they are much faster available. Therefore, if you do a lot with data, saving processing time may outweigh the extra parsing time.

+8
source

Source: https://habr.com/ru/post/949263/


All Articles