omrilotan
1 min readOct 21, 2019

--

This is true to an extent. While for objects of uncertain structure you should use more complex methods — for simple object comparison you can use stringify passing a key order as second argument.

const a = {a: 1, b: 2}
const b = {b: 2, a: 1}
console.log(
JSON.stringify(a),
JSON.stringify(b)
)
// {"a":1,"b":2} {"b":2,"a":1}
console.log(
JSON.stringify(a, Object.keys(a).sort()),
JSON.stringify(b, Object.keys(b).sort())
)
// {"a":1,"b":2} {"a":1,"b":2}

It’s not a matter of should and shouldn’t, but of knowing the interface of the methods you do use and making a conscious decision.

--

--

omrilotan
omrilotan

Responses (1)