darrachequesne / notepack

A fast Node.js implementation of the latest MessagePack spec
MIT License
75 stars 19 forks source link

Are circular references or repeated references to the same object supported? #32

Closed chetbox closed 6 months ago

chetbox commented 6 months ago

I'm attempting to serialise a large object. Currently, using JSON.stringify we're hitting a problem with the sting becomes too large. (> 512MB)

In reality, the object being serialised is a Redux state which has multiple shared references to the same objects. Does Notepack support efficiently serialising these objects by ensuring they only appear in the marshalled output once?

If it does, are circular references also supported?

I haven't been able to find this information in the documentation.

darrachequesne commented 6 months ago

No, circular references are not supported:

import { encode } from "notepack.io";

const a = {};
a.b = a;

encode(a); // throws "Uncaught RangeError: Maximum call stack size exceeded"

For what it's worth, most libraries do not support them:

import { encode } from "@msgpack/msgpack";
import { pack } from "msgpackr";

JSON.stringify(a); // throws "Uncaught TypeError: Converting circular structure to JSON"
encode(a); // throws "Uncaught Error: Too deep objects in depth 101"
pack(a); // throws "Uncaught RangeError: Maximum call stack size exceeded"
chetbox commented 6 months ago

Thanks! That's helpful to know.

I couldn't find anything mentioned about circular references. Is it worth mentioning in the readme? Hopefully at the very least search engines will crawl this issue. :)

darrachequesne commented 6 months ago

Added in the documentation: https://github.com/darrachequesne/notepack/blob/main/README.md#are-circular-references-supported

Thanks!