GetItemsOfType: Poor performance with sc:IsItemOfType()

Sitecore 6 introduced a new XSLT helper function, IsItemOfType() that will determine if your item descends from a certain type.

This is a very cool and very usefull function when developing component based websites where each page descends from different components identifying if the page has certain properties.

Unfortunately the function suffers from very bad performance which makes it useless in list iterations. See the following example that iterates over all subitems of an item and returns only those who inherits from the “News” template, identifying that the item is a news item:

<xsl:for-each select="$newsRoot//item[sc:IsItemOfType('News',.)]">
<xsl:sort select="sc:fld('News_Date',.)" data-type="text" order="descending"/>
<!-- Here I do my stuff with the items -->
<!-- Render time: 93.000 ms -->

The list contains 1057 news items in about 20 subfolders. With the above code the list takes 93 seconds(!) to render on my local machine.

Let’s try a new solution. I change the code to use @template=’news’ instead:

<xsl:for-each select="$newsRoot//item[@template='news']">
<xsl:sort select="sc:fld('News_Date',.)" data-type="text" order="descending"/>
<!-- I now use the @template='news' to get my news items -->
<!-- instead of the sc:IsItemOfType() -->
<!-- Render time: 139 ms -->

The list now takes 139 ms to render, which is approximately 669 times faster. The disadvantage by using @template=’news’ is obvious. My items has to be of that exact template, not inheriting from it.

There is another solution, and that is to use the sc:GetItemsOfType() function which will return all items from an iterator descending from a specific template:

<xsl:for-each select="sc:GetItemsOfType('News',$newsRoot//item)">
<xsl:sort select="sc:fld('News_Date',.)" data-type="text" order="descending"/>
<!-- I get the same list as the other 2 examples -->
<!-- and I do the same things -->
  <!-- Render time: 515 ms -->

This takes 515 ms, which is still 3.7 times slower than the @template=’news’ version. 515 ms could be acceptable in situations where the list can be cached. I would still recommend the middle version using the @template=’news’ for large lists. Using IsItemOfType() and GetItemsOfType() can be used for smaller lists (0-100 items probably) where the performance loss is not that visible.


About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 6 and tagged , . Bookmark the permalink.

One Response to GetItemsOfType: Poor performance with sc:IsItemOfType()

  1. Bump!
    I had notice the same issue in IsItemOfType(). Well, I am still using naming contains(@template,’news’) and then I just name templates somethingNews… I have been using this model since version 4.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.