lucagez / slow-json-stringify

The slowest stringifier in the known universe. Just kidding, it's the fastest (:
https://github.com/lucagez/slow-json-stringify
MIT License
470 stars 16 forks source link

Is SJS deterministic? #3

Closed dimitry12 closed 5 years ago

dimitry12 commented 5 years ago

Is SJS deterministic in the same sense as https://github.com/substack/json-stable-stringify is?

lucagez commented 5 years ago

Yes, thanks to its template nature. When you compile a schema, SJS will create a template and a queue. So, given two objects with the same props/values, SJS will always outputs two equal strings.

E.g.


const stringify = sjs({ a: 'string', b: 'number' });

// output template => [ { "a":" ] x1  [ ", "b": ]  x2  [ } ]

stringify({a:"hello",    b: 42    })  // {"a":"hello","b":42}

stringify({b:42,  a:"hello"  })  // {"a":"hello","b":42}

The order of the properties inside the object is irrelevant. As the template requires to be filled following the exact queue order. In this particular case [a, b]