Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to increase performance when set big value to innerHTML

I'm trying to set a huge (200K) response to innerHTML of a node. The goal is to get better time than 2.7 sec in Internet Explorer 6.

Any ideas?

like image 300
user61873 Avatar asked Apr 25 '09 09:04

user61873


1 Answers

This won't make it happen any faster but it will stop the browser from locking up on the user; they can continue to use the page while this happens in the background:

function asyncInnerHTML(HTML, callback) {
    var temp = document.createElement('div'),
        frag = document.createDocumentFragment();
    temp.innerHTML = HTML;
    (function(){
        if(temp.firstChild){
            frag.appendChild(temp.firstChild);
            setTimeout(arguments.callee, 0);
        } else {
            callback(frag);
        }
    })();
}

Using it:

var allTheHTML = '<div><a href="#">.............</div>';
asyncInnerHTML(allTheHTML, function(fragment){
    myTarget.appendChild(fragment); // myTarget should be an element node.
});

This technique will take longer than plain innerHTML but the user will be able to carry on using your site without noticing a delay.

like image 174
James Avatar answered Sep 28 '22 06:09

James