Logo

Programming-Idioms

Set boolean b to true if objects x and y contain the same values, recursively comparing all referenced elements in x and y.
Tell if the code correctly handles recursive types.
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
(def b (= x y))
(def b (identical? x y))
b = deeplyEqual(x, y));
b = x == y
import "reflect"
b := reflect.DeepEqual(x, y)
b = x == y
const arrayDeepEqual = (a, b) => a.length === b.length && a.every((x, i) => deepEqual(x, b[i]))

const deepEqual = (a, b) =>
  Array.isArray(a) && Array.isArray(b)
    ? arrayDeepEqual(a, b)
    : typeof a == 'object' && a && typeof b == 'object' && b
    ? arrayDeepEqual(Object.entries(a), Object.entries(b))
    : Number.isNaN(a) && Number.isNaN(b) || a === b

const b = deepEqual(x, y)
import {isDeepStrictEqual} from 'util'
const b = isDeepStrictEqual(x, y)
const b = JSON.stringify(x) === JSON.stringify(y);
import _ from 'underscore';
const b = _.isEqual(x, y);
$b = ($x == $y);
use Data::Compare;
my $b = Compare($x, $y);
b = x == y
b = x == y
let b = x == y;
case class A(a: Int, b: Float, c: String)
val x = A(1,2.2f,"hello")
val y = A(1,2.2f,"hello")

b = x == y