-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark-poisoning-v2.js
306 lines (288 loc) · 8.48 KB
/
benchmark-poisoning-v2.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// @ts-check
import Benchmark from "benchmark";
const NumEntries = 1000;
function onCycle(event) {
const target = event.target;
const hz = target.hz; // num runs per second
const rme = target.stats.rme; // +/-x%
const n = new Intl.NumberFormat();
console.log(
String(target) +
" ⇝ from " +
n.format((hz * (100 - rme)) / 100) +
" to " +
n.format((hz * (100 + rme)) / 100) +
" ops/sec"
);
}
// Safe apply code
// Extracted from https://github.com/dubzzz/fast-check/blob/79c5be002fa80f8478e211890e7421db302a65b0/packages/fast-check/src/utils/apply.ts
// From PR https://github.com/dubzzz/fast-check/pull/3112
const untouchedApply = Function.prototype.apply;
const ApplySymbol = Symbol("apply");
function safeExtractApply(f) {
try {
return f.apply;
} catch (err) {
return undefined;
}
}
function safeApplyHacky(f, instance, args) {
const ff = f;
ff[ApplySymbol] = untouchedApply;
const out = ff[ApplySymbol](instance, args);
delete ff[ApplySymbol];
return out;
}
function safeApply(f, instance, args) {
if (safeExtractApply(f) === untouchedApply) {
return f.apply(instance, args);
}
return safeApplyHacky(f, instance, args);
}
function buildSafeMethod(typeConstructor, methodName) {
const method = typeConstructor.prototype[methodName];
return function safe(instance, ...args) {
if (instance[methodName] === method) {
return instance[methodName](...args);
}
return safeApply(method, instance, args);
};
}
// All safe methods
// Array
const safeForEach = buildSafeMethod(Array, "forEach");
const safeIndexOf = buildSafeMethod(Array, "indexOf");
const safeJoin = buildSafeMethod(Array, "join");
const safeMap = buildSafeMethod(Array, "map");
const safeFilter = buildSafeMethod(Array, "filter");
const safePush = buildSafeMethod(Array, "push");
const safePop = buildSafeMethod(Array, "pop");
const safeSplice = buildSafeMethod(Array, "splice");
const safeSlice = buildSafeMethod(Array, "slice");
const safeSort = buildSafeMethod(Array, "sort");
// Date
const safeGetTime = buildSafeMethod(Date, "getTime");
const safeToISOString = buildSafeMethod(Date, "toISOString");
// Set
const safeAdd = buildSafeMethod(Set, "add");
// String
const safeSplit = buildSafeMethod(String, "split");
const safeStartsWith = buildSafeMethod(String, "startsWith");
const safeEndsWith = buildSafeMethod(String, "endsWith");
const safeSubstring = buildSafeMethod(String, "substring");
const safeToLowerCase = buildSafeMethod(String, "toLowerCase");
const safeToUpperCase = buildSafeMethod(String, "toUpperCase");
const safePadStart = buildSafeMethod(String, "padStart");
// Fully optimized v1
const untouchedForEach = Array.prototype.forEach;
function safeExtractForEach(instance) {
try {
return instance.forEach;
} catch (err) {
return undefined;
}
}
const safeForEachV1 = function safe(instance, ...args) {
if (safeExtractForEach(instance) === untouchedForEach) {
return instance.forEach(...args);
}
return safeApply(untouchedForEach, instance, args);
};
const untouchedIndexOf = Array.prototype.indexOf;
function safeExtractIndexOf(instance) {
try {
return instance.indexOf;
} catch (err) {
return undefined;
}
}
const safeIndexOfV1 = function safe(instance, ...args) {
if (safeExtractIndexOf(instance) === untouchedIndexOf) {
return instance.indexOf(...args);
}
return safeApply(untouchedIndexOf, instance, args);
};
// Run benchmark
// ForEach, safeForEach x3
// -> no issue, safe and unsafe versions are barely equivalent on the 3 runs
// ForEach, safeForEach, IndexOf, safeIndex x3
// -> on second run of safeForEach we observe a HUGE performance gap
// -> fixed with "Fully optimized v1"
function run() {
const allBenchmarks = () => [
new Benchmark("ForEach", () => {
const instance = [1, 2, 3];
instance.forEach(() => {});
}),
new Benchmark("safeForEach", () => {
const instance = [1, 2, 3];
safeForEach(instance, () => {});
}),
new Benchmark("safeForEach@v1", () => {
const instance = [1, 2, 3];
safeForEachV1(instance, () => {});
}),
new Benchmark("IndexOf", () => {
const instance = [1, 2, 3];
instance.indexOf(2);
}),
new Benchmark("safeIndexOf", () => {
const instance = [1, 2, 3];
safeIndexOf(instance, 2);
}),
new Benchmark("safeIndexOf@v1", () => {
const instance = [1, 2, 3];
safeIndexOfV1(instance, 2);
}),
new Benchmark("Join", () => {
const instance = [1, 2, 3];
instance.join(",");
}),
new Benchmark("safeJoin", () => {
const instance = [1, 2, 3];
safeJoin(instance, ",");
}),
new Benchmark("Map", () => {
const instance = [1, 2, 3];
instance.map(String);
}),
new Benchmark("safeMap", () => {
const instance = [1, 2, 3];
safeMap(instance, String);
}),
new Benchmark("Filter", () => {
const instance = [1, 2, 3];
instance.filter((v) => v % 2 === 0);
}),
new Benchmark("safeFilter", () => {
const instance = [1, 2, 3];
safeFilter(instance, (v) => v % 2 === 0);
}),
new Benchmark("Push", () => {
const instance = [1, 2, 3];
instance.push(4);
}),
new Benchmark("safePush", () => {
const instance = [1, 2, 3];
safePush(instance, 4);
}),
new Benchmark("Pop", () => {
const instance = [1, 2, 3];
instance.pop();
}),
new Benchmark("safePop", () => {
const instance = [1, 2, 3];
safePop(instance);
}),
new Benchmark("Splice", () => {
const instance = [1, 2, 3];
instance.splice(0, 1);
}),
new Benchmark("safeSplice", () => {
const instance = [1, 2, 3];
safeSplice(instance, 0, 1);
}),
new Benchmark("Slice", () => {
const instance = [1, 2, 3];
instance.slice(0, 1);
}),
new Benchmark("safeSlice", () => {
const instance = [1, 2, 3];
safeSlice(instance, 0, 1);
}),
new Benchmark("Sort", () => {
const instance = [1, 2, 3];
instance.sort((a, b) => a - b);
}),
new Benchmark("safeSort", () => {
const instance = [1, 2, 3];
safeSort(instance, (a, b) => a - b);
}),
new Benchmark("GetTime", () => {
const d = new Date();
d.getTime();
}),
new Benchmark("safeGetTime", () => {
const d = new Date();
safeGetTime(d);
}),
new Benchmark("ToISOString", () => {
const d = new Date();
d.toISOString();
}),
new Benchmark("safeToISOString", () => {
const d = new Date();
safeToISOString(d);
}),
new Benchmark("Add", () => {
const instance = new Set();
instance.add(4);
}),
new Benchmark("safeAdd", () => {
const instance = new Set();
safeAdd(instance, 4);
}),
new Benchmark("Split", () => {
const instance = "azertyuiop";
instance.split("i");
}),
new Benchmark("safeSplit", () => {
const instance = "azertyuiop";
safeSplit(instance, "i");
}),
new Benchmark("StartsWith", () => {
const instance = "azertyuiop";
instance.startsWith("azer");
}),
new Benchmark("safeStartsWith", () => {
const instance = "azertyuiop";
safeStartsWith(instance, "azer");
}),
new Benchmark("EndsWith", () => {
const instance = "azertyuiop";
instance.endsWith("uiop");
}),
new Benchmark("safeEndsWith", () => {
const instance = "azertyuiop";
safeEndsWith(instance, "uiop");
}),
new Benchmark("Substring", () => {
const instance = "azertyuiop";
instance.substring(1, 3);
}),
new Benchmark("safeSubstring", () => {
const instance = "azertyuiop";
safeSubstring(instance, 1, 3);
}),
new Benchmark("ToLowerCase", () => {
const instance = "azertyuiop";
instance.toLowerCase();
}),
new Benchmark("safeToLowerCase", () => {
const instance = "azertyuiop";
safeToLowerCase(instance);
}),
new Benchmark("ToUpperCase", () => {
const instance = "azertyuiop";
instance.toUpperCase();
}),
new Benchmark("safeToUpperCase", () => {
const instance = "azertyuiop";
safeToUpperCase(instance);
}),
new Benchmark("PadStart", () => {
const instance = "azertyuiop";
instance.padStart(15, "*");
}),
new Benchmark("safePadStart", () => {
const instance = "azertyuiop";
safePadStart(instance, 15, "*");
}),
];
Benchmark.invoke(
[...allBenchmarks(), ...allBenchmarks(), ...allBenchmarks()],
{ name: "run", queued: true, onCycle }
);
}
run();