From dbc7374995f70820b21355fa1db03c62c10df56c Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Wed, 4 Jan 2017 15:17:22 +0000 Subject: [PATCH] Optimise LinqBridge Count --- Src/Newtonsoft.Json/Utilities/LinqBridge.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs index f2327289..3c862729 100644 --- a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs +++ b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs @@ -711,9 +711,21 @@ namespace Newtonsoft.Json.Utilities.LinqBridge CheckNotNull(source, "source"); var collection = source as ICollection; - return collection != null - ? collection.Count - : source.Aggregate(0, (count, item) => checked(count + 1)); + if (collection != null) + { + return collection.Count; + } + + using (var en = source.GetEnumerator()) + { + int count = 0; + while (en.MoveNext()) + { + ++count; + } + + return count; + } } ///