jquery: fastest DOM insertion?

后端 未结 8 1448
长情又很酷
长情又很酷 2020-12-04 16:45

I got this bad feeling about how I insert larger amounts of HTML. Lets assume we got:

var html=\"

....
\"
相关标签:
8条回答
  • 2020-12-04 17:24

    You mention being interested in alternatives. If you look at the listing of DOM-related jQuery plugins you'll find several that are dedicated to programatically generating DOM trees. See for instance SuperFlyDom or DOM Elements Creator; but there are others.

    0 讨论(0)
  • 2020-12-04 17:25

    What are you attempting to avoid? "A bad feeling" is incredibly vague. If you have heard "the DOM is slow" and decided to "avoid the DOM", then this is impossible. Every method of inserting code into a page, including innerHTML, will result in DOM objects being created. The DOM is the representation of the document in your browser's memory. You want DOM objects to be created.

    The reason why people say "the DOM is slow" is because creating elements with document.createElement(), which is the official DOM interface for creating elements, is slower than using the non-standard innerHTML property in some browsers. This doesn't mean that creating DOM objects is bad, it is necessary to create DOM objects, otherwise your code wouldn't do anything at all.

    0 讨论(0)
  • 2020-12-04 17:27

    The answer about using a DOM fragment is on the right track. If you have a bunch of html objects that you are constant inserting into the DOM then you will see some speed improvements using the fragment. This post by John Resig explains it pretty well: http://ejohn.org/blog/dom-documentfragments/

    0 讨论(0)
  • 2020-12-04 17:29

    For starters, write a script that times how long it takes to do it 100 or 1,000 times with each method.

    To make sure the repeats aren't somehow optimized away--I'm no expert on JavaScript engines--vary the html you're inserting every time, say by putting '0001' then '0002' then '0003' in a certain cell of the table.

    0 讨论(0)
  • 2020-12-04 17:31

    I create a giant string with and then append this string with jquery. Works good and fast, for me.

    0 讨论(0)
  • 2020-12-04 17:40

    innerHTML is remarkably fast, and in many cases you will get the best results just setting that (I would just use append).

    However, if there is much already in "mydiv" then you are forcing the browser to parse and render all of that content again (everything that was there before, plus all of your new content). You can avoid this by appending a document fragment onto "mydiv" instead:

    var frag = document.createDocumentFragment();
    frag.innerHTML = html;
    $("#mydiv").append(frag);
    

    In this way, only your new content gets parsed (unavoidable) and the existing content does not.

    EDIT: My bad... I've discovered that innerHTML isn't well supported on document fragments. You can use the same technique with any node type. For your example, you could create the root table node and insert the innerHTML into that:

    var frag = document.createElement('table');
    frag.innerHTML = tableInnerHtml;
    $("#mydiv").append(frag);
    
    0 讨论(0)
提交回复
热议问题