3

I have 2 views - view1 using Mustache and view2 using Handlebars. I want to verify if my understanding is right -

While calling the render function of the views, the performance of rendering view2 will be better than view1 as I have compiled the Handlebars template in the initialize block and while rendering the view I am passing the data to the compiled template.

Whereas in case of view1 using Mustache, the template compilation and data population happens during rendering.

Please let me know if my understanding is correct. I tried to check the load time of the views and did not get any significant difference in the load time. For view1 it was 10.8 ms and view2 was 10 ms.

 var view1 = Backbone.View.extend({

            initialize:function(options){

                 Backbone.View.prototype.initialize.call(this);

                 this.tpl = options.template;

                 this.data = options.data;

            },


            render: function(){

                 $(this.el).html(Mustache.to_html(this.tpl,this.data));

            }
        });


 var view2 = Backbone.View.extend({

            initialize:function(options){

                  Backbone.View.prototype.initialize.call(this);

                  this.tpl = options.template;

                  this.handlebarstpl = Handlebars.compile(this.tpl);

                  this.data = options.data;

            },


            render: function(){

                 $(this.el).html(this.handlebarstpl(this.data));

            }
        });
4

1 回答 1

1

你的理解是正确的。与在客户端编译模板相比,预编译模板的成本更低。

你说两个视图之间有 0.8 秒的差异。虽然这个数字看起来很小,但加起来可以为您提供更快(更好?)的用户体验。一旦模板数据更大,您可能会看到两者之间的差异更显着。

应该给你一个公平的想法。

于 2013-08-09T18:23:08.530 回答