跳到主要内容

compare

2024年06月17日
柏拉文
越努力,越幸运

一、Object.is


function is(x, y) {
if (x === y) {
return x !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}

二、saeValueZero


function saeValueZero() {
return (
x === y ||
(typeof x === "number" && typeof y === "number" && isNaN(x) && isNaN(y))
);
}

三、lodash.isEqual


function isObject(value) {
return typeof value === "object" && value !== null;
}

function isEqual(value, other) {
if (!isObject(value) || !isObject(other)) {
return value === other;
}
const valueKeys = Object.keys(value);
const otherKeys = Object.keys(other);
if (valueKeys.length !== otherKeys.length) {
return false;
}
for (let key in value) {
if (!isEqual(value[key], other[key])) {
return false;
}
}
return true;
}