简体   繁体   English

JQuery grep(...) VS 原生 JavaScript filter(...) 函数性能

[英]JQuery grep(...) VS native JavaScript filter(...) function performance

I measured the execution times of those two functions:我测量了这两个函数的执行时间:

  • jQuery grep function jQuery grep函数
  • Native JavaScript filter function原生 JavaScript 过滤功能

The execution of following methods have been measured using Chrome Profiles tool:使用 Chrome Profiles 工具测量了以下方法的执行情况:

// jQuery GREP function
function alternative1(words, wordToTest) {
  return $.grep(words, function(word) {
    return wordToTest.indexOf(word) != -1;                    
  });
}

// Native javascript FILTER function        
function alternative2(words, wordToTest) {
  return words.filter(function(word) {
    return wordToTest.indexOf(word) != -1;                    
  });
}

Array of words was constructed of 1 million randomly generated strings. words数组由 100 万个随机生成的字符串构成。 Each method was run 20 times.每种方法运行 20 次。 On my surprise jQuery grep function was faster.令我惊讶的是jQuery grep 函数更快。

Execution times (20 executions):执行次数(20 次执行):

  • jQuery grep function 26,31s jQuery grep函数26,31s
  • Native JavaScript filter function 34,66s原生 JavaScript 过滤功能34,66s

You can repeate measurings on this jsFidle - it will take some time to execute so be patient.您可以在此jsFidle上重复测量 - 执行需要一些时间,因此请耐心等待。

Is there any explanation why jQuery grep function is faster then native JavaScript filter function?有什么解释为什么 jQuery grep函数比原生JavaScript 过滤器函数更快?

PS: This questions was inspired by this answer . PS:这个问题的灵感来自这个答案

By comparing the actual jQuery function $.grep uses on the page通过对比页面上实际使用的 jQuery 函数$.grep

function (a, b, c) {
    var d = [],
        e;
    c = !! c;
    for (var f = 0, g = a.length; f < g; f++) e = !! b(a[f], f), c !== e && d.push(a[f]);
    return d
}

(check here for non-minified, thanks Alexander ) against the algorithm specified for 在这里检查非缩小,感谢亚历山大)针对指定的算法

Array.prototype.filter . Array.prototype.filter

It looks to me like .filter forces its this to Object , checks the callback IsCallable and sets this in it as well as checking for existence of property in each iteration, whereas .grep assumes and skips these steps, meaning there is slightly less going on.在我看来, .filter将它的this强制为Object ,检查回调IsCallable并在其中设置this以及在每次迭代中检查属性的存在,而.grep假设并跳过这些步骤,这意味着发生的事情稍微少一些.

Combine this with how good the JavaScript compiler in Chrome is and you might find the speed difference.将此与 Chrome 中 JavaScript 编译器的出色程度结合起来,您可能会发现速度差异。

Adding some of these into $.grep would make it look like将其中一些添加到$.grep会使它看起来像

function (elems, callback, inv, thisArg) {
    var ret = [],
        retVal;
    inv = !!inv;
    for (var i = 0, length = elems.length; i < length; i++) {
        if (i in elems) { // check existance
            retVal = !!callback.call(thisArg, elems[i], i); // set callback this
            if (inv !== retVal) {
                ret.push(elems[i]);
            }
        }
    }
    return ret;
}

and take about the same time as .filter (modified Alexander's jsperf) ..filter (修改亚历山大的 jsperf)花费大约相同的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM