jQuery.grep与Array.filter的性能 [英] Performance of jQuery.grep vs. Array.filter

查看:78
本文介绍了jQuery.grep与Array.filter的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个问题中,讨论了jQuery和本机JS如何相互竞争。

In a question it was discussed on how jQuery and native JS would perform against each other.

当然,香草解决方案的执行速度要快得多,因为它不能处理整个数组,所以我建议使用 Array.filter 我非常有信心至少会比 $。grep 快。

While of course the vanilla solution performs a lot faster because it does not process the whole array I proposed the usage of Array.filter which I was pretty confident would be at least faster than $.grep.

令人惊讶的是,将其添加到测试中后上了一课: 测试套件

Surprisingly after adding it to the test I was taught a lesson: Testsuite

边缘包当然有不同的地方

任何人都知道为什么 $。grep 比本地方法快3倍以上的想法 Arrray.filter

Anyone having an idea why $.grep is supposed to be over 3 times faster than the native method Arrray.filter?

编辑:我修改了测试以使用来自MDN的过滤器填充程序,结果非常有趣:

I modified the test to use the filter shim from MDN and the results are pretty interesting:


  • Chrome:即使MDN填充程序比jQuery领先本机方法快

  • Firefox:Firefox填充符比jQuery领先本机方法慢一点

,最后出现一个类似我希望在

and finally a result like i was hoping it to see in


  • Internet Explorer中看到的结果:
    本机方法最快,然后是jQuery,匀速最慢(也许这只是IE JS引擎较弱的结果...)

推荐答案

此博客文章中找到 (也执行相同的测试):

As found on this blog post (which also does the same kind of tests):


如果您阅读了 filte r ,您将看到为什么它这么慢。

If you read the documentation for filter, you will see why it's so much slower.


  1. 它忽略删除的值和空白在数组中

  2. 它可选地设置谓词函数的执行上下文

  3. 它防止谓词函数变异数据


这篇关于jQuery.grep与Array.filter的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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