JS: Problems of testing object equality as json string

By Xah Lee. Date: . Last updated: .

If you turn object into JSON string, then compare the string, this is not reliable because object properties may be in different order.

// Test equality of two object contents as JSON.
// If 2 object have the same properties but different order, json return false.

const x = { a: 1, b: 2 };
const y = { b: 2, a: 1 };

console.assert((JSON.stringify(x) === JSON.stringify(y)) === false);