在 JavaScript 中过滤 JSON 对象列表的最高性能方法是什么? [英] What is the highest performance way to filter a list of JSON objects in JavaScript?

查看:15
本文介绍了在 JavaScript 中过滤 JSON 对象列表的最高性能方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个巨大的(1000+)这样的对象列表:

Let's assume I have a huge (1000+) list of objects like this:

[{name: 'john dow', age: 38, gender:'m'}, {name: 'jane dow', age: 18, gender:'f'}, ..]

我想按名称(字符明智)过滤此列表.

I want to filter this list by name (character wise).

filter('j') => [{name: 'john dow', age: 38, gender:'m'}, {name: 'jane dow', age: 18, gender:'f'}, ..]

filter('jo') => [{name: 'john dow', age: 38, gender:'m'}, ..]

filter('dow') => [{name: 'john dow', age: 38, gender:'m'}, {name: 'jane dow', age: 18, gender:'f'}, ..]

实现此目的的最高性能方式是什么?RegEx 显然是关键之一,如果您假设用户通常倾向于从头开始命名,则事先对列表进行排序也可能是一个好主意,但它仅在某些情况下有帮助.

What is the highest performance way to do that? RegEx is obviously one of the keys, ordering the list beforehand if you assume that user usually tend to start names from the beginning may also a good idea, but it only helps in some cases.

是否有任何用于映射过滤器的 JavaScript 内置函数?我希望它们比 JavaScript 实现更快.

Are there any JavaScript built-in functions for mapping a filter? I'd expect those to be faster than JavaScript implementations.

P.S.:是的,我想在客户端过滤,因为我想提供离线功能".

P.S.: Yes I want to filter on client side because of "offline capabilities" I want to offer.

推荐答案

虽然子字符串索引(例如后缀树) 会使速度更快,直接搜索将是:

Although a substring index (such as a Suffix tree) would make this faster, the direct search would be:

function (s, l) {
    return l.filter(function (v) {
        return v.name.find(s) !== -1;
    });
}

其中 s 是查询字符串,l 是对象列表.

where s is the query string and l is the list of objects.

这篇关于在 JavaScript 中过滤 JSON 对象列表的最高性能方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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