If you've got a container with a lot of child elements you had better empty the container element before removing it. Due to the way jQuery handles remove vs empty, empty is thousands of times faster, at least in this situation.So for each time I've used remove() since then, I've been doing empty().remove(), as it is "thousands of times faster". Well, I've been meaning to test this so here we go. I created a simple template for testing. The idea was to created a table with 5000 rows with elements nested 3 levels deep. As expected, jQuery drops these things from the DOM pretty quickly. Here is the template (I'm looping using ColdFusion, but you could recreate this in any other language:
So do this:
$('#container').empty().remove();
…instead of this:
$('#container').remove();
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function go (){
var start = new Date();
$('#tbltest').empty().remove();
var end = new Date();
alert(end - start);
}
</script>
<button onclick="go();">Remove</button>
<table id="tbltest">
<tbody>
<cfloop from="1" to="5000" index="i">
<tr>
<td><a href="javascript:void(0);"><span>Text</span></a></td>
<td><a href="javascript:void(0);"><span>Text</span></a></td>
<td><a href="javascript:void(0);"><span>Text</span></a></td>
<td><a href="javascript:void(0);"><span>Text</span></a></td>
<td><a href="javascript:void(0);"><span>Text</span></a></td>
<td><a href="javascript:void(0);"><span>Text</span></a></td>
<td><a href="javascript:void(0);"><span>Text</span></a></td>
<td><a href="javascript:void(0);"><span>Text</span></a></td>
</tr>
</cfloop>
</tbody>
</table>
Note that I'm using jQuery 1.4.2. The plan was to allow the page to load fully and then trigger the remove() or empty().remove().
First, when running this 10 times using remove():
- 869ms
- 906ms
- 907ms
- 919ms
- 889ms
- 912ms
- 948ms
- 1055ms
- 933ms
- 900ms
- 941ms
- 897ms
- 881ms
- 923ms
- 954ms
- 951ms
- 1015ms
- 968ms
- 939ms
- 962ms
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function go (){
var start = new Date();
$('#tbltest').empty().remove();
var end = new Date();
alert(end - start);
}
</script>
<button onclick="go();">Remove</button>
<table id="tbltest">
<tbody>
<cfloop from="1" to="2000" index="i">
<tr>
<td>
<table>
<tbody>
<tr>
<td>
<table>
<tbody>
<tr>
<td>
<table>
<tbody>
<tr>
<td>
<span><span><span><span><span><span><a href="javascript:void(0);"><span>Text</span></a></span></span></span></span></span></span>
</td>
<td>
<span><span><span><span><span><span><a href="javascript:void(0);"><span>Text</span></a></span></span></span></span></span></span>
</td>
<td>
<span><span><span><span><span><span><a href="javascript:void(0);"><span>Text</span></a></span></span></span></span></span></span>
</td>
<td>
<span><span><span><span><span><span><a href="javascript:void(0);"><span>Text</span></a></span></span></span></span></span></span>
</td>
<td>
<span><span><span><span><span><span><a href="javascript:void(0);"><span>Text</span></a></span></span></span></span></span></span>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tr>
</cfloop>
</tbody>
</table>
remove():
- 769ms
- 812ms
- 767ms
- 744ms
- 822ms
- 778ms
- 766ms
- 822ms
- 1018ms
- 778ms
- 997ms
- 782ms
- 792ms
- 978ms
- 1062ms
- 765ms
- 728ms
- 759ms
- 1003ms
- 834ms
A few things to call out here are that I am using jQuery 1.4.2 on Firefox 3.6.13. This matters as different results could be given in IE with it's JS engine, as well as the comment on the jQuery remove() page may have been referring to another version, although I did choose 1.4.2 to mimic the date of the post.
Nicholas:
ReplyDeleteMy guess is the person who made the comment was using jQuery 1.2.x. I seem to recall similar issues w/older versions of jQuery. The problem w/his comment is he doesn't show an example or even declare the version of jQuery he's using.
Looks to be dramatic for IE. http://jsperf.com/removing-large-tables
ReplyDelete