Native vs jQuery after

JavaScript performance comparison

Test case created by Chen-Pang He

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<ul id="test-list"><li>Hello world!</li></ul>
<script>
Benchmark.prototype.setup = function() {
    var testList = document.getElementById("test-list");
   
    function insertAfter(newElement, ref) {
      var refParent = ref.parentNode;
      var refNext = ref.nextSibling;
   
      if (refNext === null)
        refParent.appendChild(newElement);
      else
        refParent.insertBefore(newElement, refNext);
      return newElement;
    }
};

Benchmark.prototype.teardown = function() {
    var helloWorld = document.createElement("li");
    helloWorld.appendChild(document.createTextNode("Hello world!"));
   
    $(testList).empty();
    testList.appendChild(helloWorld)
};
</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
jQuery after (HTML)
$(testList.firstChild).after("<li>Hello world again!</li>");
$(testList.firstChild).after("<li>I am in the middle.</li>");
pending…
jQuery after (DOM)
var again = document.createElement("li");
var middle = document.createElement("li");

again.appendChild(document.createTextNode("Hello world again!"));
middle.appendChild(document.createTextNode("I am in the middle."));

$(testList.firstChild).after(again);
$(testList.firstChild).after(middle);
pending…
Native
var again = document.createElement("li");
var middle = document.createElement("li");

again.appendChild(document.createTextNode("Hello world again!"));
middle.appendChild(document.createTextNode("I am in the middle."));

insertAfter(again, testList.firstChild);
insertAfter(middle, testList.firstChild);
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