-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
TypedArrayBenchmark.cs
97 lines (84 loc) · 2.17 KB
/
TypedArrayBenchmark.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
using BenchmarkDotNet.Attributes;
namespace Jint.Benchmark;
[MemoryDiagnoser]
public class TypedArrayBenchmark
{
private const string script = @"
var testArray = new Int32Array([29, 27, 28, 838, 22, 2882, 2, 93, 84, 74, 7, 933, 3754, 3874, 22838, 38464, 3837, 82424, 2927, 2625, 63, 27, 28, 838, 22, 2882, 2, 93, 84, 74, 7, 933, 3754, 3874, 22838, 38464, 3837, 82424, 2927, 2625, 63, 27, 28, 838, 22, 2882, 2, 93, 84, 74, 7, 933, 3754, 3874, 22838, 38464, 3837, 82424, 2927, 2625, 63, 27, 28, 838, 22, 2882, 2, 93, 84, 74, 7, 933, 3754, 3874, 22838, 38464, 3837, 82424, 2927, 2625, 63]);
";
private Engine engine;
[GlobalSetup]
public void Setup()
{
engine = new Engine();
engine.Execute(script);
}
[Params(100)]
public int N { get; set; }
[Benchmark]
public void Slice()
{
for (var i = 0; i < N; ++i)
{
engine.Execute("testArray.slice();");
}
}
[Benchmark]
public void Concat()
{
// tests conversion performance as TypedArray does not have concat
for (var i = 0; i < N; ++i)
{
engine.Execute("[].concat(testArray);");
}
}
[Benchmark]
public void Index()
{
for (var i = 0; i < N; ++i)
{
engine.Execute(@"
var obj2 = new Int32Array(testArray.length);
for (var i = 0, l = testArray.length; i < l; i++) {
obj2[i] = testArray[i];
}
");
}
}
[Benchmark]
public void Map()
{
for (var i = 0; i < N; ++i)
{
engine.Execute(@"
var obj2 = testArray.map(function(i) {
return i;
});
");
}
}
[Benchmark]
public void Apply()
{
for (var i = 0; i < N; ++i)
{
engine.Execute("Array.apply(undefined, testArray);");
}
}
[Benchmark]
public void JsonStringifyParse()
{
for (var i = 0; i < N; ++i)
{
engine.Execute("JSON.parse(JSON.stringify(testArray));");
}
}
[Benchmark]
public void FilterWithNumber()
{
for (var i = 0; i < N; ++i)
{
engine.Execute("testArray.filter(function(i) { return i > 55; });");
}
}
}