Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8766321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:31:19+00:00 2026-06-13T16:31:19+00:00

Possible Duplicate: How do you determine equality for two JavaScript objects? Object comparison in

  • 0

Possible Duplicate:
How do you determine equality for two JavaScript objects?
Object comparison in JavaScript

If I have two arrays or objects and want to compare them, such as

object1 = [
 { shoes:
   [ 'loafer', 'penny' ]
  },
  { beers:
     [ 'budweiser', 'busch' ]
  }
]

object2 = [
 { shoes:
   [ 'loafer', 'penny' ]
  },
  { beers:
     [ 'budweiser', 'busch' ]
  }
]

object1 == object2 // false

this can be annoying if you’re getting a response from a server and trying to see if it’s changed

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:31:21+00:00Added an answer on June 13, 2026 at 4:31 pm

    Update:
    In response to the comments and worries surrounding the original suggestion (comparing 2 JSON strings), you could use this function:

    function compareObjects(o, p)
    {
        var i,
            keysO = Object.keys(o).sort(),
            keysP = Object.keys(p).sort();
        if (keysO.length !== keysP.length)
            return false;//not the same nr of keys
        if (keysO.join('') !== keysP.join(''))
            return false;//different keys
        for (i=0;i<keysO.length;++i)
        {
            if (o[keysO[i]] instanceof Array)
            {
                if (!(p[keysO[i]] instanceof Array))
                    return false;
                //if (compareObjects(o[keysO[i]], p[keysO[i]] === false) return false
                //would work, too, and perhaps is a better fit, still, this is easy, too
                if (p[keysO[i]].sort().join('') !== o[keysO[i]].sort().join(''))
                    return false;
            }
            else if (o[keysO[i]] instanceof Date)
            {
                if (!(p[keysO[i]] instanceof Date))
                    return false;
                if ((''+o[keysO[i]]) !== (''+p[keysO[i]]))
                    return false;
            }
            else if (o[keysO[i]] instanceof Function)
            {
                if (!(p[keysO[i]] instanceof Function))
                    return false;
                //ignore functions, or check them regardless?
            }
            else if (o[keysO[i]] instanceof Object)
            {
                if (!(p[keysO[i]] instanceof Object))
                    return false;
                if (o[keysO[i]] === o)
                {//self reference?
                    if (p[keysO[i]] !== p)
                        return false;
                }
                else if (compareObjects(o[keysO[i]], p[keysO[i]]) === false)
                    return false;//WARNING: does not deal with circular refs other than ^^
            }
            if (o[keysO[i]] !== p[keysO[i]])//change !== to != for loose comparison
                return false;//not the same value
        }
        return true;
    }
    

    But in many cases, it needn’t be that difficult IMO:

    JSON.stringify(object1) === JSON.stringify(object2);
    

    If the stringified objects are the same, their values are alike.
    For completeness’ sake: JSON simply ignores functions (well, removes them all together). It’s meant to represent Data, not functionality.
    Attempting to compare 2 objects that contain only functions will result in true:

    JSON.stringify({foo: function(){return 1;}}) === JSON.stringify({foo: function(){ return -1;}});
    //evaulutes to:
    '{}' === '{}'
    //is true, of course
    

    For deep-comparison of objects/functions, you’ll have to turn to libs or write your own function, and overcome the fact that JS objects are all references, so when comparing o1 === ob2 it’ll only return true if both variables point to the same object…

    As @a-j pointed out in the comment:

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

    is false, as both stringify calls yield "{"a":1,"b":2}" and "{"b":2,"a":1}" respectively. As to why this is, you need to understand the internals of chrome’s V8 engine. I’m not an expert, and without going into too much detail, here’s what it boils down to:

    Each object that is created, and each time it is modified, V8 creates a new hidden C++ class (sort of). If object X has a property a, and another object has the same property, both these JS objects will reference a hidden class that inherits from a shared hidden class that defines this property a. If two objects all share the same basic properties, then they will all reference the same hidden classes, and JSON.stringify will work exactly the same on both objects. That’s a given (More details on V8’s internals here, if you’re interested).

    However, in the example pointed out by a-j, both objects are stringified differently. How come? Well, put simply, these objects never exist at the same time:

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

    This is a function call, an expression that needs to be resolved to the resulting value before it can be compared to the right-hand operand. The second object literal isn’t on the table yet.
    The object is stringified, and the exoression is resolved to a string constant. The object literal isn’t being referenced anywhere and is flagged for garbage collection.
    After this, the right hand operand (the JSON.stringify({b: 2, a: 1}) expression) gets the same treatment.

    All fine and dandy, but what also needs to be taken into consideration is that JS engines now are far more sophisticated than they used to be. Again, I’m no V8 expert, but I think its plausible that a-j’s snippet is being heavily optimized, in that the code is optimized to:

    "{"b":2,"a":1}" === "{"a":1,"b":2}"
    

    Essentially omitting the JSON.stringify calls all together, and just adding quotes in the right places. That is, after all, a lot more efficient.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How do you determine equality for two JavaScript objects? Why does [1,[2,3]]
Possible Duplicate: Python - Determine the type of an object? I want 'complex' to
Possible Duplicate: Determine if two rectangles overlap each other? Considering I have 2 squares
Possible Duplicate: Webkit GTK: Determine when a document is finished loading I want to
Possible Duplicate: How to determine number Saturdays and Sundays comes between two dates in
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: How to determine an object's class (in Java)? Java determine which class
Possible Duplicate: How to determine the class of a generic type? I have a
Possible Duplicate: jQuery or JavaScript: Determine when image finished loading The problem is known,
Possible Duplicate: Determine framework (CLR) version of assembly I have a library/DLL file which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.