using Newtonsoft.Json.Linq;
using System.Diagnostics;
public static void Main()
RunTestOnJson(TestObjectA, "Warmup", 10);
for(int i = 0; i < 3; i++){
RunTestOnJson(TestObjectA, "Where it's already a GUID", 100);
RunTestOnJson(TestObjectB, "Where it's a string", 100);
public static void RunTestOnJson(Func<JObject> jsonFac, string description, int count)
var seqA = Enumerable.Repeat(0, count).Select(n => jsonFac()).ToArray();
Stopwatch timer = Stopwatch.StartNew();
seqA.Select(ToGuidDumb).ToList();
Console.WriteLine(description + " - dumb (ms): " + timer.ElapsedTicks);
var seqB = Enumerable.Repeat(0, count).Select(n => jsonFac()).ToArray();
timer = Stopwatch.StartNew();
seqA.Select(ToGuidClever).ToList();
Console.WriteLine(description + " - clever (ms): " + timer.ElapsedTicks);
public static Guid ToGuidDumb(dynamic json)
return Guid.Parse((string)json.Id);
public static Guid ToGuidClever(dynamic json)
public static JObject TestObjectA()
JObject json = new JObject();
json["Id"] = Guid.NewGuid();
public static JObject TestObjectB()
JObject json = new JObject();
json["Id"] = Guid.NewGuid().ToString();