Search code examples
javascriptandroidperformancecordovainnerhtml

JavaScript (adding with innerHTML to a div) Will I improve my cordova app's speed if I change this code?


this is a specific question, I won't change any code if it doesn't increase my app´s performance/speed, however I'll take advices for future developments.

I'm using Cordova 2.4.0 and developing for Android Jelly Bean 4.2.2, specifically for the SM-T110.

The JS code: (actually Works very well, but very slow) For more information, this is inside a "for" loop to generate table rows.

div.innerHTML = div.innerHTML + "Something";

If I better use appendChild... would it increase my app's speed?

Thank you.


Solution

  • The harder work in performance always is the render of the DOM. Every time you set the innerHTML you are calling the DOM.

    Better put the div.innerHTML call AFTER the loop.
    Something like this:

    myVar = ""
    for(a=0; a < yourTableLength; a++)
    {
        myVar += "Something";
    }
    div.innerHTML = myVar;