Created
June 26, 2025 09:13
-
-
Save teoadal/4389a0cab3a8bac4fc858c8105dbe788 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text.Json; | |
using System.Text.Json.Nodes; | |
using BenchmarkDotNet.Configs; | |
using Hyperbee.Json.Extensions; | |
using JsonCons.JsonPath; | |
using Newtonsoft.Json.Linq; | |
namespace ConsoleApp1; | |
[SimpleJob(RuntimeMoniker.Net90)] | |
[SimpleJob(RuntimeMoniker.Net80)] | |
[Orderer(SummaryOrderPolicy.FastestToSlowest)] | |
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByJob)] | |
[MeanColumn, MemoryDiagnoser] | |
public class JsonPathBench | |
{ | |
private string[] _filters = null!; | |
private string _json = null!; | |
[Benchmark] | |
public int JsonCons_JsonElement() | |
{ | |
using var jsonDocument = JsonDocument.Parse(_json); | |
var jsonElement = jsonDocument.RootElement; | |
var result = 0; | |
foreach (var filter in _filters) | |
{ | |
var selectResult = JsonSelector.Select(jsonElement, filter); | |
result += selectResult.Count; | |
} | |
return result; | |
} | |
[Benchmark] | |
public int JsonPathNet_JsonNode() | |
{ | |
using var jsonDocument = JsonDocument.Parse(_json); | |
var jsonNode = JsonObject.Create(jsonDocument.RootElement); | |
var result = 0; | |
foreach (var filter in _filters) | |
{ | |
var path = Json.Path.JsonPath.Parse(filter); | |
var selectResult = path.Evaluate(jsonNode); | |
result += selectResult.Matches.Count; | |
} | |
return result; | |
} | |
[Benchmark] | |
public int JsonPathway_JsonElement() | |
{ | |
using var jsonDocument = JsonDocument.Parse(_json); | |
var jsonElement = jsonDocument.RootElement; | |
var result = 0; | |
foreach (var filter in _filters) | |
{ | |
var selectResult = JsonPathway.JsonPath.ExecutePath(filter, jsonElement); | |
result += selectResult.Count; | |
} | |
return result; | |
} | |
[Benchmark(Baseline = true)] | |
public int Hyperbee_JsonElement() | |
{ | |
using var jsonDocument = JsonDocument.Parse(_json); | |
var jsonElement = jsonDocument.RootElement; | |
var result = 0; | |
foreach (var filter in _filters) | |
{ | |
var selectResult = jsonElement.Select(filter); | |
result += selectResult.Count(); | |
} | |
return result; | |
} | |
[Benchmark] | |
public int Hyperbee_JsonNode() | |
{ | |
using var jsonDocument = JsonDocument.Parse(_json); | |
var jsonNode = JsonObject.Create(jsonDocument.RootElement); | |
var result = 0; | |
foreach (var filter in _filters) | |
{ | |
var selectResult = jsonNode.Select(filter); | |
result += selectResult.Count(); | |
} | |
return result; | |
} | |
[Benchmark] | |
public int Newtonsoft_JObject() | |
{ | |
var jObject = JObject.Parse(_json); | |
var result = 0; | |
foreach (var filter in _filters) | |
{ | |
var selectResult = jObject.SelectTokens(filter); | |
result += selectResult.Count(); | |
} | |
return result; | |
} | |
[GlobalSetup] | |
public void Setup() | |
{ | |
_json = | |
""" | |
{ | |
"store": { | |
"book": [ | |
{ | |
"category": "reference", | |
"author": "Nigel Rees", | |
"title": "Sayings of the Century", | |
"price": 8.95 | |
}, | |
{ | |
"category": "fiction", | |
"author": "Evelyn Waugh", | |
"title": "Sword of Honour", | |
"price": 12.99 | |
}, | |
{ | |
"category": "fiction", | |
"author": "Herman Melville", | |
"title": "Moby Dick", | |
"isbn": "0-553-21311-3", | |
"price": 8.99 | |
}, | |
{ | |
"category": "fiction", | |
"author": "J. R. R. Tolkien", | |
"title": "The Lord of the Rings", | |
"isbn": "0-395-19395-8", | |
"price": 22.99 | |
} | |
], | |
"bicycle": { | |
"color": "red", | |
"price": 19.95 | |
} | |
} | |
} | |
"""; | |
_filters = | |
[ | |
"$", | |
"$.store.book[0]", | |
"$.store..price", | |
"$..book[0]", | |
"$.store.*", | |
"$.store.book[*].author", | |
"$.store.book[-1:]", | |
"$.store.book[0,1]", | |
"$.store.book['category','author']", | |
"$..book[?(@.isbn)]", | |
"$.store.book[?(@.price == 8.99)]", | |
"$..*", | |
"$..book[?(@.price == 8.99 && @.category == 'fiction')]" | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment