Skip to content

Commit f6d0cff

Browse files
Add JsonNode construction benchmarks (#4463)
Add JsonNode construction benchmarks
1 parent 5aa6dec commit f6d0cff

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using BenchmarkDotNet.Attributes;
2+
using MicroBenchmarks;
3+
using System.Text.Json.Nodes;
4+
5+
namespace System.Text.Json.Node.Tests
6+
{
7+
[BenchmarkCategory(Categories.Libraries, Categories.JSON)]
8+
public class Perf_Create
9+
{
10+
private readonly JsonNode[] _results = new JsonNode[50];
11+
12+
[Benchmark]
13+
public Span<JsonNode> Create_JsonBool()
14+
{
15+
Span<JsonNode> results = _results.AsSpan();
16+
for (int i = 0; i < results.Length; i++)
17+
{
18+
results[i] = (JsonNode)true;
19+
}
20+
21+
return results;
22+
}
23+
24+
[Benchmark]
25+
public Span<JsonNode> Create_JsonNumber()
26+
{
27+
Span<JsonNode> results = _results.AsSpan();
28+
for (int i = 0; i < results.Length; i++)
29+
{
30+
results[i] = (JsonNode)42;
31+
}
32+
33+
return results;
34+
}
35+
36+
[Benchmark]
37+
public Span<JsonNode> Create_JsonString()
38+
{
39+
Span<JsonNode> results = _results.AsSpan();
40+
for (int i = 0; i < results.Length; i++)
41+
{
42+
results[i] = (JsonNode)"some string";
43+
}
44+
45+
return results;
46+
}
47+
48+
[Benchmark]
49+
public Span<JsonNode> Create_JsonArray()
50+
{
51+
Span<JsonNode> results = _results.AsSpan(0, 20);
52+
for (int i = 0; i < results.Length; i++)
53+
{
54+
results[i] = new JsonArray { null, null, null, null };
55+
}
56+
57+
return results;
58+
}
59+
60+
[Benchmark]
61+
public JsonNode Create_JsonObject_Small()
62+
{
63+
return new JsonObject
64+
{
65+
["prop0"] = null,
66+
["prop1"] = null,
67+
["prop2"] = null,
68+
["prop3"] = null,
69+
["prop4"] = null,
70+
["prop5"] = null,
71+
["prop6"] = null,
72+
["prop7"] = null,
73+
["prop8"] = null,
74+
};
75+
}
76+
77+
[Benchmark]
78+
public JsonNode Create_JsonObject_Large()
79+
{
80+
return new JsonObject
81+
{
82+
["prop0"] = null,
83+
["prop1"] = null,
84+
["prop2"] = null,
85+
["prop3"] = null,
86+
["prop4"] = null,
87+
["prop5"] = null,
88+
["prop6"] = null,
89+
["prop7"] = null,
90+
["prop8"] = null,
91+
["prop9"] = null,
92+
["prop10"] = null,
93+
["prop11"] = null,
94+
["prop12"] = null,
95+
["prop13"] = null,
96+
["prop14"] = null,
97+
["prop15"] = null,
98+
["prop16"] = null,
99+
["prop17"] = null,
100+
["prop18"] = null,
101+
["prop19"] = null,
102+
};
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)