5

I am trying to understand about JSON.stringify() and often times I hear people saying that its very slow for large objects. So I would like to understand what makes JSON.stringify so slow.

After researching on the Internet, I found that the alternative to JSON.stringify() is a module called fast-json-stringify which uses a schema based technique to stringify objects.

Could you please explain how a schema based technique outperforms JSON.stringify().

Any help would be greatly appreciated.

Thanks,

Sai Raman Kilambi
  • 878
  • 2
  • 12
  • 29
  • 1
    Form [fast-json-stringify README.md](https://github.com/fastify/fast-json-stringify): "fast-json-stringify is significantly faster than JSON.stringify() **for small payloads**. Its performance advantage shrinks as your payload grows." (emphasize mine). – Kaiido Sep 21 '20 at 05:38
  • I recently noticed the extreme slowness of V8's JSON.stringify for objects with large string values - I've posted a chromium issue about it here: https://bugs.chromium.org/p/chromium/issues/detail?id=1473638 – joe Aug 17 '23 at 13:54

1 Answers1

3

JSON.stringify is a cpu bound operation.

The object subject to JSON.stringify needs to be parsed and then be transferred into an external memory block during the current thread. (heavily blocking Object:Get / Has / IsThatType or NULL / memory allocations etc) When the 'stringify' is completed, the result must be copied back to current heap. (not for free - blocks the main thread again)

xMayank
  • 1,875
  • 2
  • 5
  • 19