Fast JSON parsing with simdjson to reduce CPU usage and event loop blocking

Hiyas,

As many of you know, V8’s inbuilt JSON.parse() method is synchronous and blocks the event loop during its execution. If you frequently parse large JSON strings in your code, it can really hurt performance.

A couple of days ago, simdjson 0.3 was released. This is currently the “fastest JSON parser in the world” which can achieve a parsing speed in excess of 3GB/s.

The result is that using simdjson’s JSON.parse() instead of V8’s JSON.parse() would block the event loop much less.

simdjson is written in C++ and achieves its impressive speed by automatically leveraging the CPU’s SIMD instructions and using microparallel algorithms.

Ideally, simdjson’s code should be incorporated directly into V8 but until that happens, the next best thing is for it to be incorporated into our Meteor apps and/or Meteor itself.

simdjson can be used in Meteor projects via the nodejs binding which can be installed by running meteor npm install simdjson

Here is the most basic example:

import simdjson from 'simdjson';

const jsonString = "{   \
  \"foo\": {            \
    \"bar\": [          \
      \"baz\": 0,       \
      \"baz\": 42       \
    ]                   \
  }                     \
}"
const parsedJSON = simdjson.parse(jsonString); // parsed JSON object

However, there are more complex use cases described in the docs for maximising performance by avoiding unnecessary memory copying overhead in marshalling the JSON object between C++ and V8.

I have created a feature request for simdjson to be incorporated into Meteor if you feel like giving it a thumbs up:

Disclosure: I have not yet performed benchmarks. It is possible that V8’s JSON.parse() could still outperform simdjson on small JSON strings as it avoids the previously mentioned overheads. For larger strings, simdjson will definitely blow V8 out of the water.

1 Like