jQuery .clone(true) vs. rebinding.

JavaScript performance comparison

Test case created by George Pantazis

Preparation code

<div id="#foo">Hello World</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var x = 1
        $foo = $('#foo');
   
    var bindIt = function(){
      $foo.on('customEvent', function(){
         x++;
      });
    };
   
    bindIt();
   
    var $fooDetach = $foo.detach();
};

Benchmark.prototype.teardown = function() {
    $('#foo').off('customEvent');
    delete $foo;
    delete $fooDetach;
};
</script>

Preparation code output

Hello World

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() then rebind
var $new = $fooDetach.clone();
$('body').append($new);
bindIt();
$new.trigger('customEvent');
pending…
clone(true)
var $new = $fooDetach.clone(true);
$('body').append($new);
$new.trigger('customEvent');
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