2
0

HashBenchmark.cs 908 B

1234567891011121314151617181920212223242526272829303132333435
  1. using BenchmarkDotNet.Attributes;
  2. using BenchmarkDotNet.Configs;
  3. using BenchmarkDotNet.Order;
  4. using Jint.Extensions;
  5. namespace Jint.Benchmark;
  6. [RankColumn]
  7. [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByParams)]
  8. [Orderer(SummaryOrderPolicy.FastestToSlowest)]
  9. public class HashBenchmark
  10. {
  11. [Params("i", "str", "Math", "encodeURIComponent")]
  12. public string Input { get; set; }
  13. [Benchmark(Baseline = true)]
  14. public int StringHashCode() => Input.GetHashCode();
  15. [Benchmark]
  16. public int StringOrdinalHashCode() => StringComparer.Ordinal.GetHashCode(Input);
  17. [Benchmark]
  18. public int Fnv1() => Hash.GetFNVHashCode(Input);
  19. /*
  20. [Benchmark]
  21. public ulong Hash3()
  22. {
  23. Span<byte> s1 = stackalloc byte[Input.Length * 2];
  24. Encoding.UTF8.TryGetBytes(Input, s1, out var written);
  25. return System.IO.Hashing.XxHash3.HashToUInt64(s1[..written]);
  26. }
  27. */
  28. }