Why is JSON.stringify() slow for large objects

Viewed 865

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,

1 Answers

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)

Related