innerHTML vs replaceHtml vs $.html

JavaScript performance comparison

Test case created by DouglasDuteil and last updated



Preparation code

<script src=""></script>
<pre id="text" style="height : 200px;">
Benchmark.prototype.setup = function() {
    var ele = document.getElementById("text");
    var tenElsStr = "<b>1</b><i>2</i><b>3</b><i>4</i><b>5</b><i>6</i><b>7</b><i>8</i><b>9</b><i> </i>";
    function multiply (str, num) {
        return Array(num + 1).join(str);
    // This is much faster than simple use of innerHTML in some browsers
    // See <>
    function replaceHtml(el, html) {
      var oldEl = el;
    /*@cc_on // Pure innerHTML is slightly faster in IE
                oldEl.innerHTML = html;
                return oldEl;

      var newEl = oldEl.cloneNode(false);
      newEl.innerHTML = html;
      oldEl.parentNode.replaceChild(newEl, oldEl);
    /* Since we just removed the old element from the DOM, return a reference
        to the new element, which can be used to restore variable references. */

      return newEl;
    var testStr = multiply(tenElsStr, 1000);
    var $ele = $(ele);

Preparation code output


Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
ele.innerHTML = testStr
ele = replaceHtml(ele, testStr);

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment