在js中比较2个对象的最快方法 [英] Fastest way to compare 2 objects in js

查看:25
本文介绍了在js中比较2个对象的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 javascript 中比较 2 个对象的最快方法是什么?

Which is the fastest way to compare 2 objects in javascript?

例如我有这两个对象:

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}]
    }
}

这花费的时间太长了,有没有其他更快的方法?感谢您的时间!

This is taking too long, is it there another way faster? Thank you for your time!

推荐答案

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

You can have a look at the fast-deep-equal package. Here is a performance benchmark from their README.md for your reference.

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

这篇关于在js中比较2个对象的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆