jQuery and appending large amounts of HTML

杀马特。学长 韩版系。学妹 提交于 2019-11-29 02:23:39

There's a performance issue with jQuery and inserting a large string of html to the DOM, due to its $.trim function.

I sometimes find plain old dom scripting much faster than using jquery. Try something like

document.getElementById('id').innerHTML = myarray.join('')

Be aware that often the speed bottleneck is not creating the DOM, but inserting the DOM. This is especially true on IE with complicated style sheets and when the new content contains many levels of nested tags.

See: http://bitkickers.blogspot.com/2008/12/ajax-grid-performance-in-ie.html

Have you considered using a templating library? PURE has very good performance, for instance (try out the 500 row example).

I think you can split html in many parts and append it one by one.

$("table tr:last").after('<tr>...</tr>');

Like document lines in googleDocs

Matt Gardner

Try cloning parts of the DOM itself rather than generating it on the fly (i.e. append actual DOMElements so they don't have to be created).

i was messing around with appending large amounts of html yesterday. i think rendering on server side and inserting is the way to go, also i may add, that not using jquery is faster by 50-100ms in my tests, which are here:

http://programmingdrunk.com/playground

you will need to enable firebug console to see speed results

Chase is right, it doesn't matter how fast you can generate the HTML via JavaScript, it's the DOM insertion that will kill you. Tie any programming language with the DOM and it'll be slow.

My only suggest is to have the table paginated so you don't load so many at once, or maybe not to use AJAX at all.

In my case,document.getElementById('id').innerHTML = myarray.join('') is also a killer since array have 10 HTMl strings. The joins would create a big long string and nnerHTML performance varies significantly between 100ms to 1200ms on IE 7.

any cluses?

You would probably get better performance if you do the HTML generation on the server side, and then use Ajax to retrieve the html and append it to your DOM. (I'm assuming you're getting all the data from the server using Ajax anyway.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!