From e08d1f752462260837445399626bba3b09f31f75 Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Wed, 4 Jan 2017 15:01:48 +0000 Subject: [PATCH] Optimise ToArray for list source --- Src/Newtonsoft.Json/Utilities/LinqBridge.cs | 26 ++++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs index 2777b418..f2327289 100644 --- a/Src/Newtonsoft.Json/Utilities/LinqBridge.cs +++ b/Src/Newtonsoft.Json/Utilities/LinqBridge.cs @@ -793,17 +793,25 @@ namespace Newtonsoft.Json.Utilities.LinqBridge return new List(source); } - /// - /// Creates an array from an . - /// + /// + /// Creates an array from an . + /// - public static TSource[] ToArray( - this IEnumerable source) - { - return source.ToList().ToArray(); - } + public static TSource[] ToArray( + this IEnumerable source) + { + IList ilist = source as IList; + if (ilist != null) + { + TSource[] array = new TSource[ilist.Count]; + ilist.CopyTo(array, 0); + return array; + } - /// + return source.ToList().ToArray(); + } + + /// /// Returns distinct elements from a sequence by using the default /// equality comparer to compare values. ///