Problem, as an interviewer would state it:
"Create isEqual (deep equality) that correctly handles NaN === NaN, -0 versus +0, Dates, RegExps, arrays, plain objects, and circular references. Discuss edge cases, runtime, and alternatives."
Examples:
deepEqual(NaN, NaN); // true (=== says false)
deepEqual(0, -0); // false (=== says true)
deepEqual({ a: [1, new Date(0)] }, { a: [1, new Date(0)] }); // true
const x = { v: 1 }; x.self = x; const y = { v: 1 }; y.self = y;
deepEqual(x, y); // true, and it terminates
Clarifying questions expected:
- Which equality for primitives (
Object.is), and do prototypes and key order matter? - Should
MapandSetignore order, and how are object members matched? - What should two circular structures do, and how much data will it see?
Code / implementation expected: Yes — real code, checked against a trusted implementation and on cycles and shared structure.