clone vs dom editing

JavaScript performance comparison

Test case created by Mohit Seth

Info

I want to edit row in text. Want to test if cloning and replacing will be a good option or just editing.

Preparation code

<table id="test-table">
  <tr id='tr-1'>
    <td>
      Test
    </td>
    <td>
      Test
    </td>
  </tr>
</table>
<style>
  table{border-collapse:collapse;} tr.selected td{border:1px solid green;}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var $row = $("#tr-1");
    var rawRow = document.getElementById('tr-1');
};
</script>

Preparation code output

Test Test

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
clone
var $clonedRow = $row.clone(false);
$clonedRow.addClass('selected');
$clonedRow.find('td').each(function() {
  var text = $(this).html();
  this.innerHTML = "<input type='text' value='" + text + "' />";
  this.innerHTML = "Test";
});

$row.replaceWith($clonedRow);
pending…
direct dom editing
$row.addClass('selected');
$row.find('td').each(function() {
  var text = $(this).html();
  this.innerHTML = "<input type='text' value='" + text + "' />";
  this.innerHTML = "Test";
});
pending…
raw js clone
var clonedRow = rawRow.cloneNode(true);
clonedRow.className = 'selected';
$(clonedRow).find('td').each(function() {
  var text = $(this).html();
  this.innerHTML = "<input type='text' value='" + text + "' />";
  this.innerHTML = "Test";
});
$row.replaceWith(clonedRow);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment