Improve jQuery template performance

后端 未结 5 1228

Update

Apparently, jQuery templates can be compiled and it helps performance for templates with if statements shown here.

But a

5条回答
  •  失恋的感觉
    2021-02-19 19:51

    Chi Chan,

    a bit late on the trail with this one. I've discovered that compiling the templates first and then referencing them by a string Id (in the case below, the named variable templateAlias ) is actually 10 times faster than going via the object route. Here's how you'd achieve that (based on your code sample):

    var templateAlias = 'tmplRow';
    // compile the template
    $.template(templateAlias, $("#tmplRow"));
    
    
    

    this should significantly speed up procedings as the template will have been compiled and won't be using the entire object model everytime you run the .appendTo() function. Using $('#tmplRow').tmpl(data).appendTo('#table'); means that $('#tmplRow') queries the DOM, whereas, $.tmpl(templateAlias, data).appendTo('#table'); only adds to the DOM based on the reference to the template. there's quite a bit of reading on the subject.

    Here's a link that may help:

    http://boedesign.com/misc/presentation-jquery-tmpl/#13

    hope this helps, Good luck...

提交回复
热议问题