Ajax response handling: innerHTML vs Sandboxed Iframe

JavaScript performance comparison

Test case created by Boye

Info

Inspired by this article (http://community.jboss.org/people/wesleyhales/blog/2011/08/28/fixing-ajax-on-mobile-devices) about sanitizing Ajax repsonse data based on a sandboxed iframe, I was curious to see what the performance of this approach would be.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="content">
</div>
<script>
  function __getFrame() {
    var frame = document.getElementById("temp-frame");
    if (!frame) {
      // create frame
      frame = document.createElement("iframe");
      frame.setAttribute("id", "temp-frame");
      frame.setAttribute("name", "temp-frame");
      frame.setAttribute("seamless", "");
      frame.setAttribute("sandbox", "");
      frame.style.display = 'none';
      document.getElementById('content').appendChild(frame);
    }
    return frame.contentDocument;
  }
 
  function __createXhr(fn) {
    $.ajax({
      type: 'GET',
      url: '//jsperf.com/api',
      success: fn
    });
  }
</script>

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
innerHTML approach
// async test
__createXhr(function(data) {
  document.getElementById('content').innerHTML = data;
  deferred.resolve();
});
pending…
Sandboxed Iframe approach
// async test
__createXhr(function(data) {
  var frame = __getFrame();
  frame.write(data);
  deferred.resolve();
});
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment