-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbenchmark-array-includes-vs-regex.html
127 lines (124 loc) · 3.56 KB
/
benchmark-array-includes-vs-regex.html
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
---
title: "Benchmark Array#includes vs RegExp literal &
instance"
layout: example.njk
scripts:
- url: https://unpkg.com/[email protected]/lodash.js
- url: https://unpkg.com/[email protected]/benchmark.js
---
<div x-data="page()" class="flex flex-col w-full">
<div class="flex flex-row w-full align-start">
<button
@click="run()"
class="flex bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
>
Start Benchmark
</button>
<div class="flex py-2 px-4">
Status:
<template x-if="isRunning">
<label>Running</label>
</template>
<template x-if="!isRunning && !message">
<label>Ready</label>
</template>
<template x-if="!isRunning && message">
<label>Completed</label>
</template>
</div>
</div>
<ul>
<template x-for="entry in output" :key="entry">
<li x-text="entry"></li>
</template>
</ul>
<template x-if="message">
<div class="block mt-20 p-4 border border-solid border-gray-500 rounded">
<h3 class="font-semibold">Summary</h3>
<div x-text="message"></div>
</div>
</template>
<script>
function page() {
const array = [
"allowfullscreen",
"allowpaymentrequest",
"async",
"autofocus",
"autoplay",
"checked",
"controls",
"default",
"defer",
"disabled",
"formnovalidate",
"hidden",
"ismap",
"itemscope",
"loop",
"multiple",
"muted",
"nomodule",
"novalidate",
"open",
"readonly",
"required",
"reversed",
"selected",
"typemustmatch",
];
const regex = new RegExp(array.join("|"));
const literalRegex = /allowfullscreen|allowpaymentrequest|async|autofocus|autoplay|checked|controls|default|defer|disabled|formnovalidate|hidden|ismap|itemscope|loop|multiple|muted|nomodule|novalidate|open|readonly|required|reversed|selected|typemustmatch/;
// add tests
const match = "hidden";
const noMatch = "class";
const suite = new Benchmark.Suite();
suite
.add("RegExpInstance#test - match", function () {
regex.test(match);
})
.add("Array#includes - match", function () {
array.includes(match);
})
.add("RegExpLiteral#test - match", function () {
literalRegex.test(match);
})
.add("RegExpInstance#test - no match", function () {
regex.test(noMatch);
})
.add("Array#includes - no match", function () {
array.includes(noMatch);
})
.add("RegExpInstance#test - no match", function () {
literalRegex.test(noMatch);
});
return {
isRunning: false,
message: "",
output: [],
suite,
tests: suite.map((t) => t.name),
run() {
// cache Alpine instance under $vm (Vue.js instance).
const $vm = this;
// reset
this.message = "";
this.output = [];
// set to "running"
this.isRunning = true;
this.suite
.on("cycle", function (event) {
$vm.output = [...$vm.output, String(event.target)];
})
.on("complete", function () {
$vm.message = `Fastest is ${this.filter("fastest").map(
"name"
)}\nSlowest is ${this.filter("slowest").map("name")}`;
$vm.isRunning = false;
})
.run({ async: true });
},
};
}
</script>
</div>