Use char instead of string for DetectJson (#13019)

* Use char instead of string for DetectJson

* Use faster method

* Change DetectIsJon method
This commit is contained in:
patrickdemooij9
2022-09-30 01:39:54 +02:00
committed by GitHub
parent 826dbe7d62
commit ae5a8496bb
2 changed files with 72 additions and 2 deletions

View File

@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Umbraco.Tests.Benchmarks.Config;
namespace Umbraco.Tests.Benchmarks;
[QuickRunWithMemoryDiagnoserConfig]
public class DetectJsonBenchmarks
{
private string Input { get; set; }
[GlobalSetup]
public void Setup()
{
Input = "[{test: true},{test:false}]";
}
[Benchmark(Baseline = true)]
public bool StringDetectJson()
{
var input = Input.Trim();
return (input.StartsWith("{") && input.EndsWith("}"))
|| (input.StartsWith("[") && input.EndsWith("]"));
}
[Benchmark]
public bool CharDetectJson()
{
var input = Input.Trim();
return (input.StartsWith('{') && input.EndsWith('}'))
|| (input.StartsWith('[') && input.EndsWith(']'));
}
[Benchmark]
public bool CharRangeIndexDetectJson()
{
var input = Input.Trim();
return (input[0] is '[' && input[^1] is ']') || (input[0] is '{' && input[^1] is '}');
}
//This is the fastest, however we the check will be less good than before as it'll return true on things like this: [test or {test]
[Benchmark]
public bool CharRangeIndexDetectJsonBad()
{
var input = Input;
return input[0] is '{' or '[' || input[^1] is '}' or ']';
}
[Benchmark]
public bool CharDetectJsonTwoLookups()
{
var input = Input.Trim();
var firstChar = input[0];
var lastChar = input[^1];
return (firstChar is '[' && lastChar is ']') || (firstChar is '{' && lastChar is '}');
}
//| Method | Mean | Error | StdDev | Ratio | Allocated |
//|---------------------------- |------------:|----------:|----------:|------:|----------:|
//| StringDetectJson | 103.7203 ns | 1.5370 ns | 0.0842 ns | 1.000 | - |
//| CharDetectJson | 8.8119 ns | 1.0330 ns | 0.0566 ns | 0.085 | - |
//| CharRangeIndexDetectJson | 7.8054 ns | 1.2396 ns | 0.0679 ns | 0.075 | - |
//| CharRangeIndexDetectJsonBad | 0.4597 ns | 0.1882 ns | 0.0103 ns | 0.004 | - |
//| CharDetectJsonTwoLookups | 7.8292 ns | 1.7397 ns | 0.0954 ns | 0.075 | - |
}