Should you use JSON.stringify() for objects or not ?

Objects are reference types, so you can’t use your normal equality operators, i.e. == or ===. as you use for comparison of primitives.

As the name suggests JSON.stringify() converts objects to strings first and comparison takes place afterward.

JSON.stringify({ a: 1, b: 2}) === "{"a":1,"b":2}"

JSON.stringify({ b: 2, a: 1}) === "{"b":2,"a":1}"

For JSON. stringify() , the order matters.

So if the key-value pair are ordered differently in the two objects but are the same, it will return false. Whereas it doesn’t matter in Lodash isEqual , it will return true as along as the key-value pair exists.

Also, it would return false for objects that were deeply equal, but whose keys were entered in a different order:

Alternatives for Comparison

The JavaScript community has some other methods for object comparison:


By default, the sort() function sorts values as strings.

This works well for strings (“Apple” comes before “Banana”).

However, if numbers are sorted as strings, “25” is bigger than “100”, because “2” is bigger than “1”.

When the sort() function compares two values, it sends the values to the compare function, and sorts the values according to the returned (negative, zero, positive) value.

If the result is negative a is sorted before b.

If the result is positive b is sorted before a.

If the result is 0 no changes are done with the sort order of the two values.


The compare function compares all the values in the array, two values at a time (a, b).

When comparing 40 and 100, the sort() method calls the compare function(40, 100).

The function calculates 40–100 (a - b), and since the result is negative (-60), the sort function will sort 40 as a value lower than 100.

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store