比较js中2个对象的最快方法

时间:2019-01-21 14:09:44

标签: javascript angular angular6

哪种方法是比较javascript中2个对象的最快方法?

例如,我有以下两个对象:

a = [{'name': 'john', 'age': 22}, {'name': 'mike', 'age': 23}, {'name': 'anne', 'age': 12}, {'name': 'dan', 'age': 29}, {'name': 'jane', 'age': 34}]
b = [{'name': 'john', 'age': 22}, {'name': 'anne', 'age': 12}]

通常,我会这样做:

for (var i = 0; i < a.length; i++) {
    for (var j = 0; j < b.length; j++) {
        console.log(a[i]) // => [{'name': 'john', 'age': 22}, {'name': 'anne', 'age': 12}]
}

这花费了太长时间,还有另一种更快的方法吗?谢谢您的时间!

}

5 个答案:

答案 0 :(得分:2)

我没有足够的代表来添加评论来解释 Neha Mundra 的代码,所以我想我会改为发布。

let a={a:20,b:10};
let b={b:10,a:20};

console.log(Object.entries(a).sort().toString()===
            Object.entries(b).sort().toString()) //true
  1. Object.entries(a) / Object.entries(b) 返回对象的内容
  2. .sort() 按键值 ('a'/'b') 对它们进行排序,正如您在示例中看到的那样,键在声明时被反转,这将确保它们在相同的比较顺序。
  3. toString 返回对象的字符串表示。
  4. === 运算符正在检查两个字符串是否相等。

希望这可能对某人有所帮助。如果您不确定发生了什么,也可以在浏览器的开发控制台中进行检查。

答案 1 :(得分:1)

您可以查看fast-deep-equal软件包。这是他们的README.md的性能基准,供您参考。

fast-deep-equal x 226,960 ops/sec ±1.55% (86 runs sampled)
nano-equal x 218,210 ops/sec ±0.79% (89 runs sampled)
shallow-equal-fuzzy x 206,762 ops/sec ±0.84% (88 runs sampled)
underscore.isEqual x 128,668 ops/sec ±0.75% (91 runs sampled)
lodash.isEqual x 44,895 ops/sec ±0.67% (85 runs sampled)
deep-equal x 51,616 ops/sec ±0.96% (90 runs sampled)
deep-eql x 28,218 ops/sec ±0.42% (85 runs sampled)
assert.deepStrictEqual x 1,777 ops/sec ±1.05% (86 runs sampled)
ramda.equals x 13,466 ops/sec ±0.82% (86 runs sampled)
The fastest is fast-deep-equal

答案 2 :(得分:0)

let a={a:20,b:10};
let b={b:10,a:20};

console.log(Object.entries(a).sort().toString()===
            Object.entries(b).sort().toString()) //true

答案 3 :(得分:0)


// this comparison would not work for function comparisons
// this would only work best for compared objects that do not belong to the same address in memory
// Returns true if there is no difference, and false otherwise



export const isObjSame = (obj1, obj2) => {
    if (typeof obj1 !== "object" && obj1 !== obj2) {
        return false;
    }

    if (typeof obj1 !== "object" && typeof obj2 !== "object" && obj1 === obj2) {
        return true;
    }

    if (typeof obj1 === "object" && typeof obj2 === "object") {
        if (Array.isArray(obj1) && Array.isArray(obj2)) {
            if (obj1.length === obj2.length) {
                if (obj1.length === 0) {
                    return true;
                }
                const firstElemType = typeof obj1[0];

                if (typeof firstElemType !== "object") {
                    const confirmSameType = (currentType) =>
                        typeof currentType === firstElemType;

                    const checkObjOne = obj1.every(confirmSameType);
                    const checkObjTwo = obj2.every(confirmSameType);

                    if (checkObjOne && checkObjTwo) {
                        // they are primitves, we can therefore sort before and compare by index
                        // use number sort
                        // use alphabet sort
                        // use regular sort
                        if (firstElemType === "string") {
                            obj1.sort((a, b) => a.localeCompare(b));
                            obj2.sort((a, b) => a.localeCompare(b));
                        }
                        obj1.sort((a, b) => a - b);
                        obj2.sort((a, b) => a - b);

                        let equal = true;

                        obj1.map((element, index) => {
                            if (!isObjSame(element, obj2[index])) {
                                equal = false;
                            }
                        });

                        return equal;
                    }

                    if (
                        (checkObjOne && !checkObjTwo) ||
                        (!checkObjOne && checkObjTwo)
                    ) {
                        return false;
                    }

                    if (!checkObjOne && !checkObjTwo) {
                        for (let i = 0; i <= obj1.length; i++) {
                            const compareIt = isObjSame(obj1[i], obj2[i]);
                            if (!compareIt) {
                                return false;
                            }
                        }

                        return true;
                    }

                    // if()
                }
                return isObjSame(obj1, obj2);
            } else {
                return false;
            }
        }

        if (!Array.isArray(obj1) && !Array.isArray(obj2)) {
            let equal = true;
            if (obj1 && obj2) {
                const allKeys1 = Array.from(Object.keys(obj1));
                const allKeys2 = Array.from(Object.keys(obj2));

                if (allKeys1.length === allKeys2.length) {
                    allKeys1.sort((a, b) => a - b);
                    allKeys2.sort((a, b) => a - b);

                    allKeys1.map((key, index) => {
                        if (
                            key.toLowerCase() !== allKeys2[index].toLowerCase()
                        ) {
                            equal = false;
                            return;
                        }

                        const confirmEquality = isObjSame(obj1[key], obj2[key]);

                        if (!confirmEquality) {
                            equal = confirmEquality;
                            return;
                        }
                    });
                }
            }

            return equal;

            // return false;
        }
    }
};

答案 4 :(得分:-1)

a = [{'name': 'john', 'age': 22}, {'name': 'mike', 'age': 23}, {'name': 'anne', 'age': 12}, {'name': 'dan', 'age': 29}, {'name': 'jane', 'age': 34}]
b = [{'name': 'john', 'age': 22}, {'name': 'anne', 'age': 12}]

var areTheyEqual = JSON.stringify(a) === JSON.stringify(b) ;

console.log('Are they equal: ' + areTheyEqual)