Thursday, June 11, 2015

Sitecore and GlassMapper - how much a GlassCast<> costs

We are using the GlassMapper on many projects, I just want to know how it affects performance.
I have about 20000 Items for testing (Articles) with about 20 fields, in first code I use the GlassCast to get Titles:


items.Select(item => item.GlassCast<Article>()).Select(art => art.Title).ToList();



In second part I will use the field directly:


items.Select(item => item[Article.TitleFieldName]).ToList();



In first case, when we use the GlassCast average time was 00:00:55.23 In second case average time was 00:00:00.034

It's almost 2000 times faster, so we should use it carefully!

3 comments:

  1. Option 1 is just bad code in my opinion. When you cast something you need a reason to do so. GlassCasting something means you're in need of the extra data of the entity you're loading. Cause that's why it takes so long, you're loading a substantial amount of data and abstraction while the data you need can be found on the basic indexer of the item without any effort. Option 2 has no magic strings so it's perfectly fine in my opinion.

    ReplyDelete
  2. I ran a similar test on Synthesis and posted about it: http://kamsar.net/index.php/2015/06/Synthesis-Object-Mapping-Performance/

    ReplyDelete