String Performance

time to read 1 min | 155 words

NHibernate just got a patch that replace this line:

partString.ToLower(CultureInfo.InvariantCulture).IndexOf(text);

With this:

partString.IndexOf(text, StringComparison.InvariantCultureIgnoreCase);

This is done to increase perfromance in a slow part of the code. Looking at this, can you spot the reason for the performance increase between the two?

ToLower() will create a new string (which require memory allocation) use if for a single call and discard it (which means the GC now has to collect it). The second approach doesn't create temporary objects and does not require memory allocation for potentially large object.