What is the most efficient way to create HTML elements using jQuery?

后端 未结 12 2463
甜味超标
甜味超标 2020-11-22 06:40

Recently I\'ve been doing a lot of modal window pop-ups and what not, for which I used jQuery. The method that I used to create the new elements on the page has overwhelming

相关标签:
12条回答
  • 2020-11-22 06:51

    personally i'd suggest (for readability):

    $('<div>');
    

    some numbers on the suggestions so far (safari 3.2.1 / mac os x):

    var it = 50000;
    
    var start = new Date().getTime();
    for (i = 0; i < it; ++i)  {
      // test creation of an element 
      // see below statements
    }
    var end = new Date().getTime();
    alert( end - start );                
    
    var e = $( document.createElement('div') );  // ~300ms
    var e = $('<div>');                          // ~3100ms
    var e = $('<div></div>');                    // ~3200ms
    var e = $('<div/>');                         // ~3500ms              
    
    0 讨论(0)
  • 2020-11-22 06:52

    Actually, if you're doing $('<div>'), jQuery will also use document.createElement().

    (Just take a look at line 117).

    There is some function-call overhead, but unless performance is critical (you're creating hundreds [thousands] of elements), there isn't much reason to revert to plain DOM.

    Just creating elements for a new webpage is probably a case in which you'll best stick to the jQuery way of doing things.

    0 讨论(0)
  • 2020-11-22 06:56

    You'll have to understand that the significance of element creation performance is irrelevant in the context of using jQuery in the first place.

    Keep in mind, there's no real purpose of creating an element unless you're actually going to use it.

    You may be tempted to performance test something like $(document.createElement('div')) vs. $('<div>') and get great performance gains from using $(document.createElement('div')) but that's just an element that isn't in the DOM yet.

    However, in the end of the day, you'll want to use the element anyway so the real test should include f.ex. .appendTo();

    Let's see, if you test the following against each other:

    var e = $(document.createElement('div')).appendTo('#target');
    var e = $('<div>').appendTo('#target');
    var e = $('<div></div>').appendTo('#target');
    var e = $('<div/>').appendTo('#target');
    

    You will notice the results will vary. Sometimes one way is better performing than the other. And this is only because the amount of background tasks on your computer change over time.

    Test yourself here

    So, in the end of the day, you do want to pick the smallest and most readable way of creating an element. That way, at least, your script files will be smallest possible. Probably a more significant factor on the performance point than the way of creating an element before you use it in the DOM.

    0 讨论(0)
  • 2020-11-22 06:56

    One point is that it may be easier to do:

    $("<div class=foo id=bar style='color:white;bgcolor:blue;font-size:12pt'></div>")
    

    Then to do all of that with jquery calls.

    0 讨论(0)
  • 2020-11-22 07:02

    Someone has already made a benchmark: jQuery document.createElement equivalent?

    $(document.createElement('div')) is the big winner.

    0 讨论(0)
  • 2020-11-22 07:03

    This is not the correct answer for the question but still I would like to share this...

    Using just document.createElement('div') and skipping JQuery will improve the performance a lot when you want to make lot of elements on the fly and append to DOM.

    0 讨论(0)
提交回复
热议问题