-
Notifications
You must be signed in to change notification settings - Fork 0
/
timing.html
82 lines (74 loc) · 1.6 KB
/
timing.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
<html>
<head>
</head>
<body>
<button id="button" type="button">Run Tests</button>
<script type="text/javascript">
(function() {
let counter = 0;
const regex = /^test/;
const match = 'test';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const TOTAL_STRINGS = 100000;
const tests = {
regex: function() {
var x = 0;
while (x < TOTAL_STRINGS) {
if (!regex.test(possible)) {
counter++;
}
x += 1;
}
return this;
},
indexOf: function() {
var x = 0;
while (x < TOTAL_STRINGS) {
if (possible.indexOf(match) !== 0) {
counter++;
}
x += 1;
}
return this;
},
startsWith: function() {
var x = 0;
while (x < TOTAL_STRINGS) {
if (!possible.startsWith(match)) {
counter++;
}
x += 1;
}
return this;
},
substr: function() {
var x = 0;
while (x < TOTAL_STRINGS) {
if (possible.substr(0, match.length) !== match) {
counter++;
}
x += 1;
}
return this;
}
};
function runTest() {
console.time('regex');
tests.regex();
console.timeEnd('regex');
console.time('indexOf');
tests.indexOf();
console.timeEnd('indexOf');
console.time('startsWith');
tests.startsWith();
console.timeEnd('startsWith');
console.time('substr');
tests.substr();
console.timeEnd('substr');
console.log(counter);
}
document.getElementById('button').onclick = runTest;
})();
</script>
</body>
</html>