Jquery is the fastest way to delete all rows from a very large table

I thought this could be a quick way to delete the contents of a very large table (3000 rows):

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

But it will take about five seconds to complete in firefox. Am I doing something dumb (besides trying to load 3000 lines in a browser)? Is there a faster way to do this?

+81
javascript jquery dom
Apr 6 '09 at 20:36
source share
8 answers
 $("#your-table-id").empty(); 

It's as fast as you.

+191
Apr 6 '09 at 20:39
source share

It is better to avoid any loops, just delete all the elements directly as follows:

 $("#mytable > tbody").html(""); 
+76
Nov 10 2018-11-11T00: 11
source share

Using detach is much faster than any other answer here:

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

Remember to put the tbody element back in the table, as detach deleted it:

 $('#mytable').append($('<tbody>')); 

Also note that with the effectiveness of $(target).find(child) syntax of $(target).find(child) is faster than $(target > child) . What for? Sizzle!

Elapsed time to empty 3,161 rows of the table

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

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

Using the empty () method:

  • Firefox: 0.055 s
  • Chrome: 0.052 s
  • Edge: 137.99 (may be frozen)
  • IE11: freezes and never returns
+5
Nov 07 '18 at 22:19
source share

Two questions that I see here:

  • The empty () and remove () jQuery methods actually do a little work. See John Resig JavaScript Function for Profiling Calls for what.

  • Another thing is that for a large amount of tabular data, you can consider the datagrid library, such as the excellent DataTables to download data on the fly from the server, increasing the number of network calls, but reducing the size of these calls. I had a very complicated table with 1,500 rows that were pretty slow, switching to a new AJAX based table made the same data look pretty fast.

+3
Apr 10 '09 at 17:54
source share

if you want to remove only quickly .. you can do as below.

 $( "#tableId tbody tr" ).each( function(){ this.parentNode.removeChild( this ); }); 

but there may be some event related items in the table,

in this case

the above code does not prevent memory leak in IE ... TT and not fast in FF ...

sorry....

0
Oct 06 '09 at 5:27
source share
 $("#myTable > tbody").empty(); 

This will not affect the headlines.

0
Jun 18 '19 at 18:27
source share

this works for me:

1- add a class for each row "removeRow"

2- in jQuery

 $(".removeRow").remove(); 
-one
Oct 28 '13 at 9:29
source share

You can try this ...

 var myTable= document.getElementById("myTable"); if(myTable== null) return; var oTBody = myTable.getElementsByTagName("TBODY")[0]; if(oTBody== null) return; try { oTBody.innerHTML = ""; } catch(e) { for(var i=0, j=myTable.rows.length; i<j; i++) myTable.deleteRow(0); } 
-2
Apr 19 2018-12-12T00:
source share



All Articles