-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
ListInteropBenchmark.cs
99 lines (84 loc) · 2.35 KB
/
ListInteropBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System.Collections;
using BenchmarkDotNet.Attributes;
using Jint.Native;
using Jint.Runtime.Interop;
namespace Jint.Benchmark;
[MemoryDiagnoser]
public class ListInteropBenchmark
{
private static bool IsArrayLike(Type type)
{
if (typeof(IDictionary).IsAssignableFrom(type))
{
return false;
}
if (typeof(ICollection).IsAssignableFrom(type))
{
return true;
}
foreach (var typeInterface in type.GetInterfaces())
{
if (!typeInterface.IsGenericType)
{
continue;
}
if (typeInterface.GetGenericTypeDefinition() == typeof(IReadOnlyCollection<>)
|| typeInterface.GetGenericTypeDefinition() == typeof(ICollection<>))
{
return true;
}
}
return false;
}
private const int Count = 10_00;
private Engine _engine;
private JsValue[] _properties;
[GlobalSetup]
public void Setup()
{
_engine = new Engine();
_properties = new JsValue[Count];
var input = new List<Data>(Count);
for (var i = 0; i < Count; ++i)
{
input.Add(new Data { Category = new Category { Name = i % 2 == 0 ? "Pugal" : "Beagle" } });
_properties[i] = JsNumber.Create(i);
}
_engine.SetValue("input", input);
_engine.SetValue("CONST", new { category = "Pugal" });
}
[Benchmark]
public void Filter()
{
var value = (Data) _engine.Evaluate("input.filter(i => i.category?.name === CONST.category)[0]").ToObject();
if (value.Category.Name != "Pugal")
{
throw new InvalidOperationException();
}
}
[Benchmark]
public void Indexing()
{
_engine.Evaluate("for (var i = 0; i < input.length; ++i) { input[i]; }");
}
[Benchmark]
public void HasProperty()
{
var input = (ObjectWrapper) _engine.GetValue("input");
for (var i = 0; i < _properties.Length; ++i)
{
if (!input.HasProperty(_properties[i]))
{
throw new InvalidOperationException();
}
}
}
private class Data
{
public Category Category { get; set; }
}
private class Category
{
public string Name { get; set; }
}
}