Is JSON.stringify() deterministic in V8?

前端 未结 4 1029
梦谈多话
梦谈多话 2021-02-19 02:27

I\'ve not seen (yet?) JSON.stringify to be non-deterministic in Node.JS.

There is no guarantee it to be deterministic on the specification level.

4条回答
  •  不要未来只要你来
    2021-02-19 02:56

    Determinism in your terms boil down to these:

    1. Order => Will the Object data be marshaled in the same order?

    Yes, the traversal through the object data happen in the same 'route' always.

    1. Content => Will the Object data be marshaled with same content?

    Yes, unless the arbitrariness introduced through toJSON overrides as @jmrk explained above.

    1. Concurrency => Will the Object data be modified between the checks?

    No, V8 script runner is single threaded, so no cluttered access happen.

    1. Specification => Are there clauses in the spec which violates determinism?

    No, apart from the contextual replacers / overrides, the parser and stringify SHOULD produce same data everytime.

    1. Compatibility => Will all the stringify methods produce compatible data?

    No, the spec is not clear on the order of listing Object fields so implementations are free to iterate through objects which means the data may be same to the 'purpose' and 'spirit', not comparable byte-to-byte.

    Hope this helps!

提交回复
热议问题