diff --git a/src/benchmarks/micro/libraries/System.Text.Json/Node/Perf_Create.cs b/src/benchmarks/micro/libraries/System.Text.Json/Node/Perf_Create.cs new file mode 100644 index 00000000000..16df68360e1 --- /dev/null +++ b/src/benchmarks/micro/libraries/System.Text.Json/Node/Perf_Create.cs @@ -0,0 +1,105 @@ +using BenchmarkDotNet.Attributes; +using MicroBenchmarks; +using System.Text.Json.Nodes; + +namespace System.Text.Json.Node.Tests +{ + [BenchmarkCategory(Categories.Libraries, Categories.JSON)] + public class Perf_Create + { + private readonly JsonNode[] _results = new JsonNode[50]; + + [Benchmark] + public Span Create_JsonBool() + { + Span results = _results.AsSpan(); + for (int i = 0; i < results.Length; i++) + { + results[i] = (JsonNode)true; + } + + return results; + } + + [Benchmark] + public Span Create_JsonNumber() + { + Span results = _results.AsSpan(); + for (int i = 0; i < results.Length; i++) + { + results[i] = (JsonNode)42; + } + + return results; + } + + [Benchmark] + public Span Create_JsonString() + { + Span results = _results.AsSpan(); + for (int i = 0; i < results.Length; i++) + { + results[i] = (JsonNode)"some string"; + } + + return results; + } + + [Benchmark] + public Span Create_JsonArray() + { + Span results = _results.AsSpan(0, 20); + for (int i = 0; i < results.Length; i++) + { + results[i] = new JsonArray { null, null, null, null }; + } + + return results; + } + + [Benchmark] + public JsonNode Create_JsonObject_Small() + { + return new JsonObject + { + ["prop0"] = null, + ["prop1"] = null, + ["prop2"] = null, + ["prop3"] = null, + ["prop4"] = null, + ["prop5"] = null, + ["prop6"] = null, + ["prop7"] = null, + ["prop8"] = null, + }; + } + + [Benchmark] + public JsonNode Create_JsonObject_Large() + { + return new JsonObject + { + ["prop0"] = null, + ["prop1"] = null, + ["prop2"] = null, + ["prop3"] = null, + ["prop4"] = null, + ["prop5"] = null, + ["prop6"] = null, + ["prop7"] = null, + ["prop8"] = null, + ["prop9"] = null, + ["prop10"] = null, + ["prop11"] = null, + ["prop12"] = null, + ["prop13"] = null, + ["prop14"] = null, + ["prop15"] = null, + ["prop16"] = null, + ["prop17"] = null, + ["prop18"] = null, + ["prop19"] = null, + }; + } + } +} \ No newline at end of file