在 JavaScript 中过滤 JSON 对象列表的最高性能方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13565751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the highest performance way to filter a list of JSON objects in JavaScript?
提问by wzr1337
Let's assume I have a huge (1000+) list of objects like this:
让我们假设我有一个巨大的(1000+)这样的对象列表:
[{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'}, ..]
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.
什么是最高性能的方式来做到这一点?RegEx 显然是关键之一,如果您假设用户通常倾向于从头开始命名,则事先对列表进行排序也可能是一个好主意,但它仅在某些情况下有帮助。
Are there any JavaScript built-in functions for mapping a filter? I'd expect those to be faster than JavaScript implementations.
是否有任何用于映射过滤器的 JavaScript 内置函数?我希望它们比 JavaScript 实现更快。
P.S.: Yes I want to filter on client side because of "offline capabilities" I want to offer.
PS:是的,我想在客户端过滤,因为我想提供“离线功能”。
采纳答案by Dan D.
Although a substring index(such as a Suffix tree) would make this faster, the direct search would be:
尽管子字符串索引(例如Suffix tree)会使速度更快,但直接搜索将是:
function (s, l) {
return l.filter(function (v) {
return v.name.find(s) !== -1;
});
}
where sis the query string and lis the list of objects.
其中s是查询字符串,l是对象列表。
回答by Ja?ck
From experience, the following algorithm works quite well:
根据经验,以下算法效果很好:
When the user types the first letter, you perform a search using
Array.filter()perhaps and store that result under whatever the user types (e.g. "j");When the user types another letter (e.g. "o"), you perform the search on whatever was typed before ("j"), reducing the number of items to go through
When the user deletes one or more characters you try to find the stored searches based on whatever is left in the search box; if all fails, you show an empty list and invalidate the previously stored searches.
当用户输入第一个字母时,您使用
Array.filter()可能执行搜索并将结果存储在用户输入的任何内容下(例如“j”);当用户键入另一个字母(例如“o”)时,您将搜索之前键入的任何内容(“j”),从而减少要浏览的项目数
当用户删除一个或多个字符时,您尝试根据搜索框中剩余的内容查找存储的搜索;如果一切都失败了,您将显示一个空列表并使之前存储的搜索无效。
回答by owksley
I wouldn't worry too much about performance in this case. A desktop computer should eat up 1000, or 10,000 evaluations without sweat. I would avoid any kind of complex optimisation because the risk of breaking functionality is probably higher than the benefit of slightly efficient processing.
在这种情况下,我不会太担心性能。一台台式电脑应该吃掉 1000 或 10,000 次评估而不出汗。我会避免任何类型的复杂优化,因为破坏功能的风险可能高于稍微高效处理的好处。
Javascript (ECMAScript 5) does provide new methods for filtering arrays. As a native method it is supposed to be a little faster.
Javascript (ECMAScript 5) 确实提供了过滤数组的新方法。作为本机方法,它应该更快一点。
var regex = ...
results = json.filter(function(result) {
return regex.test(result.name)
}
Array.prototype.filter is supported in modern browsers, see http://kangax.github.com/es5-compat-table/. A patch for older browsers is can be added with this: https://github.com/kriskowal/es5-shim
Array.prototype.filter 在现代浏览器中受支持,请参阅http://kangax.github.com/es5-compat-table/。可以添加旧浏览器的补丁:https: //github.com/kriskowal/es5-shim

