#1114 enhancement
Viktor Kojouharov

Optimizations to the Template class

Reported by Viktor Kojouharov | August 8th, 2010 @ 09:57 PM

I've attached a patch that rewrites the Template class in order to speed it up. It passes the unit tests in Firefox and Chromium, and is ~ 3.5 times faster in my test, which evaluates a template 10_000 times.

Comments and changes to this ticket

  • Viktor Kojouharov

    Viktor Kojouharov August 9th, 2010 @ 08:16 AM

    A slightly updated patch that removes the 'startsWith' and uses 'lastIndexOf' directly, since it can potentially be called many times.

  • Viktor Kojouharov

    Viktor Kojouharov August 9th, 2010 @ 11:43 AM

    Updated the diff to work around browsers that don't support proper splitting (read: IE)

  • Viktor Kojouharov

    Viktor Kojouharov August 10th, 2010 @ 10:41 AM

    The new diff simplifies the match pattern, reduces the iterations and adds more tests which broke the previous diff.

  • Andrew Dupont

    Andrew Dupont August 15th, 2010 @ 02:11 AM

    • State changed from “new” to “enhancement”
    • Assigned user set to “Andrew Dupont”
    • Importance changed from “” to “Low”

    Good work. It might be a little late to get this into 1.7, but it can surely go into 1.7.1.

    If you don't have IE handy, feel free to post on the prototype-core mailing list to solicit testing help from PC users.

  • Viktor Kojouharov

    Viktor Kojouharov August 29th, 2010 @ 01:08 AM

    I've updated the patch with a few modifications I've been using locally.

    The biggest change is the addition to a few conditionals (#if and #for) and some logical operators. The following example illustrates them:

    { #if (condition1) }

    Some text

    { #else if (condition2 == (condition3 == 'constant' || !condition4)) }

    Another piece of text

    { #else }

    #{ #for item (array) }

     Evaluation of #{item}
     #{ #if (item == "5") }
     #{ #else if (item > '10') }
     #{ #endif }
     Trailing text.

    #{ #endfor }

    { #endif }

    (disclaimer: I just typed the above template by hand, so it might contain spelling/grammar/syntax errors)

    Regarding the evaluation of the conditional expressions, absolutely no operator precedence is taken into account. All operators are evaluated from left to right. Precedence can be accomplished using parenthesis.

    Nested #ifs and #fors are supported. Constants are enclosed with "'" or '"'.

    The other change brought by this patch is tokenizing the template in the constructor. With this, an even greater part of the work is done only once, and now the evaluate method only evaluates the tokenized template. And while some speed is lost due to the addition of conditionals (my tests indicate a consistent 0.0666ms for the original Proto template, vs 0.0666 to 0.1333ms for this, for only 1 template evaluation, in all browsers). This becomes faster than the original as soon as the template is evaluated for the second time.

  • Viktor Kojouharov
  • Andrew Dupont

    Andrew Dupont August 29th, 2010 @ 07:20 AM

    Would you mind putting the functionality enhancements in a separate ticket? They need to be considered separately from the optimizations you've written. For instance, the "tokenization in the constructor" part should stay on this ticket, but adding conditionals is a different sort of enhancement altogether. (I wonder if we should have an "optimization" ticket state to better draw this distinction.)

  • Viktor Kojouharov

    Viktor Kojouharov August 29th, 2010 @ 02:08 PM

    This is the patch without the conditionals and operators

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins


Referenced by