jquery - fastest way to remove all rows from a very large table

前端 未结 8 599
臣服心动
臣服心动 2021-01-30 09:40

I thought this might be a fast way to remove the contents of a very large table (3000 rows):

$jq(\"tbody\", myTable).remove();

But it\'s taking

8条回答
  •  既然无缘
    2021-01-30 10:30

    Using detach is magnitudes faster than any of the other answers here:

    $('#mytable').find('tbody').detach();
    

    Don't forget to put the tbody element back into the table since detach removed it:

    $('#mytable').append($(''));  
    

    Also note that when talking efficiency $(target).find(child) syntax is faster than $(target > child). Why? Sizzle!

    Elapsed Time to Empty 3,161 Table Rows

    Using the Detach() method (as shown in my example above):

    • Firefox: 0.027s
    • Chrome: 0.027s
    • Edge: 1.73s
    • IE11: 4.02s

    Using the empty() method:

    • Firefox: 0.055s
    • Chrome: 0.052s
    • Edge: 137.99s (might as well be frozen)
    • IE11: Freezes and never returns

提交回复
热议问题