Grep vs Filter in jQuery?

前端 未结 5 442
天命终不由人
天命终不由人 2020-12-13 11:36

I was wondering about the differences between Grep and Filter :

Filter :

Reduce the set of matched elements to those that match the selector

相关标签:
5条回答
  • 2020-12-13 12:27

    Filter is part of jQuery.fn so it's aim is to be used with selector $('div').filter where grep is a jQuery tool method (jQuery.grep)

    0 讨论(0)
  • 2020-12-13 12:29

    @Matas Vaitkevicius, the code snippet posted presents errors, here is a corrected one:

    function test(){
    var array = [];
    for(var i = 0; i<1000000; i++)
    {
        array.push(i);
    }
    
    var filterResult = []
    for (var i = 0; i < 1000; i++){
        var stime = new Date();
        var filter = array.filter(o => o == 99999);
        filterResult.push(new Date() - stime);
    }
    
    var grepResult = [];
    for (var i = 0; i < 1000; i++){
        var stime = new Date();
        var grep = $.grep(array,function(i,o){
            return o == 99999;
        });
        grepResult.push(new Date() - stime);
    }
    
    $('p').text('average filter - '+(filterResult.reduce((pv,cv)=>{ return pv +cv},0)/1000))
    $('div').text('average grep - '+(grepResult.reduce((pv,cv)=>{ return pv + cv},0)/1000))
    }
    test();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <p></p>
    <div></div>

    TLDR : In firefox, filter is slightly faster, in chrome, that's the opposite. Regarding performances only, you can use anyone.

    0 讨论(0)
  • 2020-12-13 12:32

    They both function in similar ways however they differ in their usages.

    The filter function is intended to be used with html elements, and that is why it is a chainable function that returns a jQuery object and it accepts filters like ":even", ":odd" or ":visible" etc. You can't do that with the grep function, which is intended to be a utility function for arrays.

    0 讨论(0)
  • 2020-12-13 12:37

    The difference in its usage:

    Filter:

    $(selector).filter(selector/function)
    

    Grep:

    $.grep(array,function,invert)
    

    So in your case I would rather use grep() because using array this way is unnecessary: $(arr).

    I also suppose that grep function is faster, because it only accepts arrays.

    0 讨论(0)
  • 2020-12-13 12:39

    To those that are interested on how grep performs against filter I wrote this test:

    TLDR; Grep is many times faster.

    Script I used for testing:

    function test(){
    var array = [];
    for(var i = 0; i<1000000; i++)
    {
    array.push(i);
    }
    
    var filterResult = []
    for (var i = 0; i < 1000; i++){
    var stime = new Date();
    var filter = array.filter(o => o == 99999);
    filterResult.push(new Date() - stime);
    }
    
    var grepResult = [];
    var stime = new Date();
    var grep = $.grep(array,function(i,o){
    return o == 99999;
    });
    grepResult.push(new Date() - stime);
    
    $('p').text('average filter - '+(filterResult.reduce((pv,cv)=>{ return pv +cv},0)/1000))
    $('div').text('average grep - '+(grepResult.reduce((pv,cv)=>{ return pv + cv},0)/1000))
    }
    test();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <p></p>
    <div></div>

    0 讨论(0)
提交回复
热议问题