Recently at work, the discussion arose if the number of fields mapped from Sitecore Items to our models would have a performance impact. The question was if we should limit the number of mapped properties in our IGlassBase interface to gain performance.
I did a simple test by mapping an item to a test interface (IGlassBaseTest) and measuring the execution time of these lines 50’000 times.
stopwatch.Start(); var item = context.GetItem<IGlassBaseTest>("/sitecore/content/GlassTest"); var theID = item.Id; stopwatch.Stop();
Test 1: Bare minimum IGlassBaseTest
public interface IGlassBaseTest { [SitecoreId] Guid Id { get; } }
Total time for 50’000 cycles: 34ms
Test 2: Extended Interface
I’ve added the [SitecoreItem] attribute guessing it might have an impact as it needs to map the entire Sitecore Item to a property.
public interface IGlassBaseTest { [SitecoreId] Guid Id { get; } [SitecoreItem] Item Item { get; } }
Total time for 50’000 cycles: 39ms
Considering the large amount of cycles, the 5ms difference is irrelevant. In some measurements the Test 2 scenario was actually even faster than Test 1.
Test 3: Fully blown interface
And now for the whole thing.
public interface IGlassBaseTest { [SitecoreId] Guid Id { get; } [SitecoreInfo(SitecoreInfoType.FullPath)] string FullPath { get; } [SitecoreInfo(SitecoreInfoType.DisplayName)] string DisplayName { get; } [SitecoreInfo(SitecoreInfoType.Name)] string Name { get; } [SitecoreInfo(SitecoreInfoType.Language)] Language Language { get; } [SitecoreInfo(SitecoreInfoType.TemplateId)] ID TemplateId { get; } [SitecoreInfo(SitecoreInfoType.TemplateName)] string Template { get; } [SitecoreInfo(SitecoreInfoType.Url)] string Url { get; } [SitecoreChildren(IsLazy = true)] IEnumerable Children { get; } [SitecoreParent(InferType = true, IsLazy = true)] IGlassBase Parent { get; } [SitecoreItem] Item Item { get; } }
Total time for 50’000 cycles: 49ms
Conclusion
While the first two tests didn’t show a significant difference, the last test adds roughly 10ms to our total. But remeber, that’s only 0.0002ms per execution which can also be considered insignificant in my opinion.
Based on these tests I can say, that the number of fields mapped by Glass Mapper only has a negligible impact on performance.