0

I'm trying to make my app run as fast as possible.

At the moment, when it runs a search, it builds the results by creating each element dynamically, like:

(outside each loop)

var theList = [];

(inside each loop):

var titleH3 = $('<h3>').append(result.title),
    jobResult = $('<div>').addClass('job-result').attr({
                        'id': result.id,
                        'data-jobno': jobNo,
                    });
//a bunch of other info also created here (about 6 elements total)

jobResult.append(titleH3);
theList.push(jobResult);

then again outside loop:

$('#load-more').before(theList)

my question is, would it be quicker to have a div already in the html and clone it each time and change the info (title etc)?

rpsep2
  • 3,061
  • 10
  • 39
  • 52
  • 1
    The difference is minuscule. The bottleneck is not there. – JJJ Aug 27 '13 at 11:44
  • 1
    http://stackoverflow.com/questions/327047/what-is-the-most-efficient-way-to-create-html-elements-using-jquery follow the link to the benchmark if you are interested. It showed that using `$(document.createElement('div'))` was quite a bit faster than `$('
    ')` at the very least
    – DGS Aug 27 '13 at 11:45
  • Thanks! didn't even know about that method – rpsep2 Aug 27 '13 at 11:53

0 Answers0