• .NET Developer Tools DevTools

    UI controls for ASP.NET AJAX, MVC, WPF,
    Silverlight, Windows 8 and Windows Phone

  • Hybrid Mobile Development Icenium

    Cross-platform Mobile Development Tool
    with cloud-based architecture

  • HTML5 / JavaScript Development Kendo UI

    Everything you need to build sites and
    mobile apps with JavaScript and HTML5

  • Testing Tools TestStudio

    One easy tool for Functional, Performance,
    Load and Mobile software testing

  • Web Presence Platform Sitefinity CMS

    Everything for your online business - content
    management, ecommerce, emarketing

  • Agile Project Management TeamPulse

    Simple and intuitive project management
    and collaboration software

Contact us

We are here for you.
  • usa+1‒888‒365‒2779
  • uk+44‒20‒7291‒0580
  • bg+359‒2‒8099850
  • de+49‒89‒2441642‒70
  • au+61‒2‒8090‒1465
  • emailsales@telerik.com
Your account Access to your products, updates and support
Telerik Product Families
  • Your Account
    Your Account
    Log in
  • ABOUT US

    About Telerik

    • Company
    • Press Center
    • Customers
    • Community
    • Careers
    • Contacts
Kendo UI - The way of HTML5
Products ▼
Kendo UI Web Kendo UI Mobile Kendo UI DataViz Server Side Wrappers
Demos Purchase Download
Blogs Documentation
Support ▼
Premium Forums StackOverflow Forums
Resources ▼

Featured Resource

Kendo UI Dojo


Blogs Code Library Demos Documentation FAQ Testing
Premium Forums Roadmap User Voice Videos Webinars More Resources
Contact Us Search
 

Blogs Archive

Kendo UI Templates: Faster than a speeding Resig

Friday, August 26, 2011 by Kendo UI Team Blog | Comments 6

One of the key components included in Kendo UI is a very high-performance JavaScript "micro-templating" implementation. Almost every JavaScript front-end development project needs templating, so consistent with Kendo UI's goal of providing everything you need for JavaScript development in a seamless, compact package, we've included a templating solution out-of-the-box.

But it's not just any templating solution. It's an unbelievably fast templating solution.

Kendo UI Templating Basics

Before we talk about the speed, let's briefly look at some Kendo UI templating basics. Kendo UI templates focus on giving you just enough templating features to accomplish the common tasks needed for common UI rendering scenarios without hurting front-end performance. As we'll see in a moment, not all JavaScript templating engines are created equal, and sometimes convenient syntax sugar comes at a price. There are three templating "constructs" covered by Kendo UI templates:

  1. Rendering raw values
  2. Rendering HTML-encoded values
  3. Executing arbitrary JavaScript template expressions

Rendering Raw Values

One of the most basic templating tasks is rendering a value as-is in a template. With Kendo UI templates, we do this:

var t = kendo.template("<div id="box"><#= firstName #></div>");

 

This approach will create a "compiled" in-line template that's ready for rendering with data. Consuming the template looks like this:

var data = { firstName: "Todd" };
t(data);

//HTML Output:
//<div id="box">Todd</div>

 

Rendering HTML-encoded Values

If you want to render an encoded HTML value in a template, Kendo UI templates can automatically handle the encoding. To do that, we use a slightly different syntax:

var t = kendo.template("<div id="box">${ firstName }</div>");

 

Now, when we consume our template, assuming there are HTML characters in the data, we'll produce this output:

var data = { firstName: "<b>Todd</b>" };
t(data);

//HTML Output:
//<div id="box">&lt;b&gt;Todd&lt;/b&gt;</div>

 

External Templates and Expressions

Finally, it's also common in templates to include expressions. Some templating frameworks invent their own re-implementation of JavaScript to provide expression sugar (at the cost of performance), but Kendo UI templates opt to simply allow the execution of any JavaScript and favor performance over expensive syntax sugar.

For example, we can use JavaScript and Kendo UI templates to define this external template for rendering a list of items:

<script id="javascriptTemplate" type="text/x-kendo-template">
    <ul>
    <# for (var i = 0; i < data.length; i++) { #>
        <li><#= data[i] #></li>
    <# } #>
    <ul>
<script>

 

To then consume this external template with an expression, we simply need to initialize a template that uses this definition and receives an array of values:

//Get the external template definition
var t = kendo.template($("#javascriptTemplate").html());

//Create some dummy data
var d = ["Todd", "Steve", "Burke"];

//Execute the template
t(d);

//HTML Output:
//<ul>
//<li>Todd</li>
//<li>Steve</li>
//<li>Burke</li>
//</ul>

 

Speed, Speed, Speed

There is no shortage of JavaScript templating libraries. Mustache. Handlebars. Underscore. jQuery Templates. They are all good in their own way, and each has its fans. So why is Kendo UI creating its own templating approach rather than using one of these libraries? Three reasons:

  1. Completeness: Kendo UI needs to included everything you need to build JavaScript apps and sites, so we need to include templating out-of-the-box. We don't want to force BYO* (Bring Your Own *Anything). Thus, we need a library that can be easily packaged with Kendo UI.
  2. Control: Along those same lines, we need to minimize external dependencies so we can quickly evolve Kendo UI based on your feedback (our release cycles are much faster than most Open Source projects!). Using an external templating library could hurt our ability to quickly respond to change requests.
  3. Performance: And most importantly, we want to make sure that the templating that is included with Kendo UI helps you build the fastest JavaScript/HTML front-ends possible.

We did not make the decision to build our own templating lightly. We know it means more work for Kendo UI, but we refuse to cut corners. To guarantee the best performance and experience, we've researched and delivered a highly-optimized micro-templating engine.

"How fast?" you ask. See for yourself in this live JSPerf test.

Depending on the browser and computer (this all runs on the client machine, so naturally there are variances), Kendo UI Templates are up to 60 times faster than jQuery Templates. That's 6000% faster! What's the engineering feat?

During our pursuit of the fastest templating money can buy, we learned a few things:

  • The JavaScript god that is John Resig has one of the best "core" micro-templating philosophies in his aptly titled "JavaScript Micro-Templating." This is our starting point and baseline. If we can meet or exceed John's speed, we're meeting our goal for fast templating.
  • While John uses array Push and Join functions to build his templates, we discovered that simple string concatenation (+=) performs even faster in many browsers, especially Chrome. (For browsers where it is not faster, a future version of Kendo UI Templates will "fallback" to Join/Push, delivering the best of both worlds.)
  • Further, and this is the biggest speed booster, we found that eliminating the JavaScript "with" block inside of the template builder delivers a MASSIVE performance improvement. Of course, some templates need the scope-helping "with" block, so Kendo UI templates use "with" blocking by default, but it can be easily disabled. When you don't need it, you can realize the full speed gains in your template rendering.

The JSPerf linked above compares Kendo UI templates (with and without the "with" option) to jQuery Templates, Handlebar, Mustache, Underscore JS, and John's near-original micro-templating implementation. Thanks to Browserscope aggregated results, we can see how Kendo UI fares across browsers in this comparison (bigger numbers are better):

image

As you can see, in most browsers Kendo UI templates crush other templating solutions when "with" is not used. Even when the "with" block is used, though, Kendo UI templates are always faster than jQuery Templates. And Kendo UI templates are only going to get faster. A coming improvement makes Kendo UI "with" performance even better in many browsers, too.

A modified version of Resig's micro-templates with no "with" block perform very well, too, especially on iOS, but Kendo UI templates is a close second. We'll try to optimize even more for RTW.

Using Kendo UI templates without the "with" block requires a simple configuration override. For a simple in-line template, the code looks like this:

var t = kendo.template("<div id="box"><#= firstName #></div>",{useWithBlock:false});

 

You can try a live demo of Kendo UI Templates configured in the same manner used in the JSPerf test by running this saved JSFiddle example.

And that is why Kendo UI is delivering it's own templating implementation.

But I love jQuery Templates…

We recognized from the outset that even though our templating implementation is fast, some people will always prefer their templating library of choice. Maybe they like the syntax sugar. Maybe they like using something familiar. Maybe they just like slower JavaScript apps (I kid, I kid…).

Kendo UI was designed to support this behavior. For anything in Kendo UI that has an out-of-the-box implementation (like Templating), we provide "escape hatches" that make it easy to use any JavaScript library you love. We don't require BYO*, but we do allow and embrace BYO* for developers that have strong preferences.

So if you love jQuery Templates or Mustache or whatever, great! You can use that with Kendo UI, too.

Syntax Debate

The one lingering question in the Kendo UI beta for templates is syntax. We're not huge fans of the admittedly "heavy" angle bracket-based template delimiters. But we need your input! What should the Kendo UI template syntax look like?

One leading idea now in the Kendo UI forums is a new "Hash" template syntax that uses the "#" character. For example:

//Raw value
kendo.template("<div id="box">#= firstName #</div>");

//Encoded value
kendo.template("<div id="box">#: firstName #</div>");

//External template & expression
<ul>
    # for (var item in items) { #
          <li> #= item # </li>
    # } #
</ul>

 

What do you think? Is this an improved syntax? Sound-off in the comments and in the forums to help shape the final Kendo UI template syntax before RTW later this year.

Kendo UI templates provides high-performance templating out-of-the-box. It's just one small piece of the Kendo UI toolset, but it highlights our commitment to doing things right, maximizing JavaScript performance. Enjoy Kendo UI templates and be sure to share your feedback!

About the Author
Todd Anglin is an avid HTML5, CSS3, and JavaScript advocate, and geek about all things web development. He is an active speaker and author, helping developers around the world learn and adopt HTML5. Todd works for Telerik as Chief Evangelist where his current technical focus in on Kendo UI. Todd is @toddanglin on Twitter.

Tags

  • performance
  • templates

6 Comments

  1. 1 TexasJetter 26 Aug 2011
    With regards to the syntax debate- I agree that the angle bracket is not the best, especially if you have something different when encoding. Also it tends to get lost in the html markup. I guess since jQuery uses the $, Microsoft is using the @, java is using the {} you are not left with much else ;)  

    I think which character is not really significant as long as it is easily identifiable from the resr of the markup.
  2. 2 Ben 24 Dec 2011
    Which jQuery templating is being used as the comparison? 
  3. 3 Anoop 24 Jul 2012
    How will this templating work if I have to use within a grid column and I am using MVVM.
  4. 4 AXL 06 Aug 2012
    when is this available? is there any news or progress now?
  5. 5 Wormhole 08 Aug 2012
    The only problem is that the include file for Kendo UI is over 1MB!  So while the templating is faster, it takes much longer to load the page.
  6. 6 William Grant 21 Mar 2013
    Get out of my chromebook, now !

Comment

  1. Click to add

  2. Click to add

  3. Click to add

  4.    
     
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
     
      
       
Blogs feed
Categories

  • Tutorials (26)
  • Release (33)
  • Browsers (7)
  • Extensions (4)
  • Tip of the Week (10)
  • Videos (6)
  • Concepts and Theory (14)
  • Misc. (25)
  • Framework Constructs (6)
  • Mobile (6)
  • UI Widgets (5)
  • Blogs (1)
  • KendoUI Labs (1)
Archive
  • 2013 June (6)
  • 2013 May (10)
  • 2013 April (10)
  • 2013 March (9)
  • 2013 February (12)
  • 2013 January (10)
  • 2012 December (9)
  • 2012 November (11)
  • 2012 October (6)
  • 2012 September (7)
  • 2012 August (8)
  • 2012 July (10)
  • 2012 June (8)
  • 2012 May (10)
  • 2012 April (7)
  • 2012 March (13)
  • 2012 February (10)
  • 2012 January (6)
  • 2011 December (10)
  • 2011 November (4)
  • 2011 October (6)
  • 2011 September (5)
  • 2011 August (9)
Home Web Mobile DataViz Server Wrappers Whitepapers Surveys Chrome Icenium Contact Us

Kendo UI framework is developed by Telerik - a leading provider of UI components for web, desktop and mobile applications. Trusted by over 100,000 customers worldwide for our devotion to quality and industry-best technical support, Telerik helps professionals maximize their productivity and "deliver more than expected" every day.

kendoui - powered by html5, css3 & jquery
get social
  • Twitter
  • Facebook
  • Google plus
  • RSS
Privacy Policy | Branding Guidelines
Powered by Sitefinity CMS

Copyright © 2011 - 2013 Telerik Inc. All rights reserved.