Is it fine to use JSON.stringify for deep comparisons and cloning?

后端 未结 3 850
时光说笑
时光说笑 2020-12-10 12:09

After attempting several implementations for deep comparison and copying for JSON-serializable objects, I\'ve noticed the fastest often are just:

function de         


        
3条回答
  •  庸人自扰
    2020-12-10 12:50

    JavaScript does not guarantee the order of keys.

    If they are entered in the same order, this approach would work most of the time, but it would not be reliable.

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

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

提交回复
热议问题