-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.html
426 lines (379 loc) · 20.6 KB
/
benchmark.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Efficiently Merging Arrays in JavaScript</title>
<style type="text/css">
[v-cloak] { display:none; }
</style>
</head>
<body>
<div class="d-flex flex-column vh-100">
<nav class="navbar navbar-dark bg-primary">
<div class="container-fluid">
<span class="navbar-brand">Efficiently Merging Arrays in JavaScript</span>
</div>
</nav>
<div id="testApp" v-cloak>
<div class="d-flex flex-column flex-fill">
<div class="border-bottom px-2 py-3">
<div class="row mx-0">
<div class="col-2 pe-4">
<div>Setup the Arrays</div>
<div class="text-muted">
Specify how many elements should be in each Array.
</div>
</div>
<div class="col-10">
<div class="d-flex mb-3">
<div class="me-5">
<div class="mb-3">
<label for="array1CountField" class="form-label">Array 1</label>
<input type="number" class="form-control" id="array1CountField" placeholder="1" v-model="array1ElementCount">
<div class="form-text">Enter a positive number.</div>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="array1RadioOptions" id="array1ValueType" value="values" v-model="array1ElementType">
<label class="form-check-label" for="array1ValueType">Use primitive values</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="array1RadioOptions" id="array1ObjectType" value="objects" v-model="array1ElementType">
<label class="form-check-label" for="array1ObjectType">Use objects</label>
</div>
</div>
<div>
<div class="mb-3">
<label for="array1CountFiel2" class="form-label">Array 2</label>
<input type="number" class="form-control" id="array1CountFiel2" placeholder="1" v-model="array2ElementCount">
<div class="form-text">Enter a positive number.</div>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="array2RadioOptions" id="array2ValueType" value="values" v-model="array2ElementType">
<label class="form-check-label" for="array2ValueType">Use primitive values</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="array2RadioOptions" id="array2ObjectType" value="objects" v-model="array2ElementType">
<label class="form-check-label" for="array2ObjectType">Use objects</label>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="border-bottom px-2 py-3">
<div class="row mx-0">
<div class="col-2 pe-4">
<div>Configure the Test</div>
<div class="text-muted">Specify how many times you want to run the test.</div>
</div>
<div class="col-10">
<div class="mb-3">
<div class="w-50 d-flex align-items-center">
<input type="range" class="flex-grow-1 form-range w-75" id="iterationSlider" min="0" max="1000" v-model="iterationsToRun">
<input type="number" class="ms-3 form-control w-25" id="iterationSliderField" placeholder="1" v-model="iterationsToRun">
</div>
<div class="form-text">{{ iterationsToRunLabel }}</div>
</div>
<ul class="list-inline">
<li class="list-inline-item">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="useConcatCheckbox" v-model="useConcat">
<label class="form-check-label" for="useConcatCheckbox">Use concat() approach</label>
</div>
</li>
<li class="list-inline-item">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="usePushCheckbox" v-model="usePush">
<label class="form-check-label" for="usePushCheckbox">Use push() approach</label>
</div>
</li>
<li class="list-inline-item">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="useSpreadCheckbox" v-model="useSpread">
<label class="form-check-label" for="useSpreadCheckbox">
Use spread approach
</label>
</div>
</li>
<li class="list-inline-item">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="useSpreadC2heckbox" v-model="useSpread2">
<label class="form-check-label" for="useSpread2Checkbox">
Use spread 2 approach
</label>
</div>
</li>
</ul>
<button class="btn btn-primary" style="white-space: nowrap;" @click="runTests">Run Tests</button>
</div>
</div>
</div>
<div class="flex-fill px-2 py-3">
<div class="row mx-0">
<div class="col-2 pe-4">
<div>Review the Results</div>
<button class="btn btn-link p-0" @click="clearResults">clear results</button>
</div>
<div class="col-10">
<div v-if="testRuns.length">
<div class="card mb-3" v-for="(testRun, runIndex) in testRuns">
<div class="card-body">
<div class="h4 card-title">Test Run #{{ testRun.number }}</div>
<p class="text-muted">total run time: {{ testRun.runtime }} ms.</p>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Iteration #</th>
<th v-if="useConcat">Concat Runtime</th>
<th v-if="usePush">Push Runtime</th>
<th v-if="useSpread">Spread Runtime</th>
<th v-if="useSpread2">Spread 2 Runtime</th>
</tr>
</thead>
<tbody>
<tr v-for="(iteration, iterationIndex) in testRun.iterations">
<td>{{ iteration.number }}</td>
<td v-if="useConcat" :class="getPerformanceClasses(iteration, testRun.iterations, 'concatRuntime')">{{ formatRuntime(iteration.concatRuntime) }}</td>
<td v-if="usePush" :class="getPerformanceClasses(iteration, testRun.iterations, 'pushRuntime')">{{ formatRuntime(iteration.pushRuntime) }}</td>
<td v-if="useSpread" :class="getPerformanceClasses(iteration, testRun.iterations, 'spreadRuntime')">{{ formatRuntime(iteration.spreadRuntime) }}</td>
<td v-if="useSpread2" :class="getPerformanceClasses(iteration, testRun.iterations, 'spread2Runtime')">{{ formatRuntime(iteration.spread2Runtime) }}</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Average</td>
<td v-if="useConcat" data-bs-toggle="tooltip" data-bs-placement="bottom" :title="testRun.avgConcatRuntime">{{ testRun.avgConcatRuntimeFixed }} ms.</td>
<td v-if="usePush" data-bs-toggle="tooltip" data-bs-placement="bottom" :title="testRun.avgPushRuntime">{{ testRun.avgPushRuntimeFixed }} ms.</td>
<td v-if="useSpread" data-bs-toggle="tooltip" data-bs-placement="bottom" :title="testRun.avgSpreadRuntime">{{ testRun.avgSpreadRuntimeFixed }} ms.</td>
<td v-if="useSpread2" data-bs-toggle="tooltip" data-bs-placement="bottom" :title="testRun.avgSpread2Runtime">{{ testRun.avgSpread2RuntimeFixed }} ms.</td>
</tr>
</tfoot>
</table>
</div>
<div class="text-muted">
This test run relied on generated arrays. The same arrays were used for every iteration and every approach.
Cells outlined in <span class="border border-success">green</span> are the fastest for the <span class="fst-italic">iteration</span>.
Cells outline in <span class="border border-danger">red</span> are the slowest for the <span class="fst-italic">iteration</span>.
The cell with the <span class="bg-success text-white">green</span> background is the fastest across all iterations.
The cell with the <span class="bg-danger text-white">red</span> background is the slowest across all iterations.
</div>
</div>
</div>
</div>
<div v-else>
<div class="alert alert-secondary" role="alert">
Specify how many elements should be in each array, configure how you want to test, and click "Run Tests"
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://unpkg.com/vue@next"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script type="text/javascript">
const worker = new Worker('./benchmark-worker.js');
const TestApp = {
mounted() {
worker.onmessage = (e) => {
const command = e.data.command;
if (command === 'initialize-arrays-completed') {
this.runNextTestIteration();
} else if (command === 'test-iteration-completed') {
const iteration = e.data.iteration;
this.onTestIterationCompleted(iteration);
}
};
},
data() {
return {
iterationsToRun: 100,
iterationNumber: 0,
currentIteration: {},
currentRun: {},
testRuns: [],
useConcat: true,
usePush: true,
useSpread: true,
useSpread2: true,
array1ElementType: 'values',
array1ElementCount: 1000,
array2ElementType: 'values',
array2ElementCount: 1000,
};
},
computed: {
iterationsToRunLabel() {
if (this.iterationsToRun === 1) {
return 'The test will run once.';
} else if (this.iterationsToRun === 0) {
return 'The test will not run.';
} else {
return `The tests will run ${this.iterationsToRun} times.`;
}
}
},
methods: {
/**
* Reacts to the user clicking the "clear results" link.
*/
clearResults() {
this.testRuns = [];
},
/**
* Reacts to the user click the "Read File" button.
*/
runTests() {
if (this.iterationsToRun > 0) {
this.iterationNumber = 0;
this.currentRun = {
startTime: performance.now(),
number: (this.testRuns.length + 1),
iterations: []
};
this.testRuns.push(this.currentRun);
const message = {
command:'initialize-arrays',
array1Length:this.array1ElementCount,
array1Type:this.array1ElementType,
array2Length:this.array2ElementCount,
array2Type:this.array2ElementType
};
worker.postMessage(message);
}
},
/**
* Starts a test iteration
*/
runNextTestIteration() {
this.iterationNumber = this.iterationNumber + 1;
this.currentIteration = { number: this.iterationNumber };
const message = { command:'run-iteration', number: this.iterationNumber, useConcat:this.useConcat, usePush:this.usePush, useSpread:this.useSpread, useSpread2:this.useSpread2 };
worker.postMessage(message);
},
/**
* Reacts to the completion of a test iteration.
*/
onTestIterationCompleted(iteration) {
this.currentRun.iterations.push(iteration);
if (iteration.number < this.iterationsToRun) {
this.runNextTestIteration();
} else {
const runtime = performance.now() - this.currentRun.startTime;
this.currentRun.runtime = parseFloat(runtime.toFixed(3));
const iterations = this.currentRun.iterations;
if (this.useConcat) {
const mean = iterations.reduce((total, r) => total + r.concatRuntime, 0) / iterations.length;
this.currentRun.avgConcatRuntime = mean;
this.currentRun.avgConcatRuntimeFixed = parseFloat(mean.toFixed(5));
}
if (this.usePush) {
const mean = iterations.reduce((total, r) => total + r.pushRuntime, 0) / iterations.length;
this.currentRun.avgPushRuntime = mean;
this.currentRun.avgPushRuntimeFixed = parseFloat(mean.toFixed(5));
}
if (this.useSpread) {
const mean = iterations.reduce((total, r) => total + r.spreadRuntime, 0) / iterations.length;
if (isNaN(mean)) {
this.currentRun.avgSpreadRuntime = 'Could not be determined.';
this.currentRun.avgSpreadRuntimeFixed = 'N/A';
} else {
this.currentRun.avgSpreadRuntime = mean;
this.currentRun.avgSpreadRuntimeFixed = parseFloat(mean.toFixed(5));
}
}
if (this.useSpread2) {
const mean = iterations.reduce((total, r) => total + r.spread2Runtime, 0) / iterations.length;
if (isNaN(mean)) {
this.currentRun.avgSpread2Runtime = 'Could not be determined.';
this.currentRun.avgSpread2RuntimeFixed = 'N/A';
} else {
this.currentRun.avgSpread2Runtime = mean;
this.currentRun.avgSpread2RuntimeFixed = parseFloat(mean.toFixed(5)); }
}
}
},
/**
* Formats the runtime shown in the UI.
*/
formatRuntime(runtime) {
let result = '0 ms.';
if (runtime) {
if (runtime === 'N/A') {
result = 'N/A';
} else {
result = runtime + ' ms.';
}
}
return result;
},
/**
* Builds a list of the class names based on the current cell's value.
*/
getPerformanceClasses(iteration, results, propertyName) {
// Identify overall best/worst performing
const value = iteration[propertyName];
const allRuntimes = [];
results.forEach(r => {
if (r.concatRuntime > 0) { // This is a guard to ensure a value of "0" is not highlighted.
allRuntimes.push(r.concatRuntime);
}
if (r.pushRuntime > 0) { // This is a guard to ensure a value of "0" is not highlighted.
allRuntimes.push(r.pushRuntime);
}
if (r.spreadRuntime > 0) { // This is a guard to ensure a value of "0" is not highlighted.
allRuntimes.push(r.spreadRuntime);
}
if (r.spread2Runtime > 0) { // This is a guard to ensure a value of "0" is not highlighted.
allRuntimes.push(r.spread2Runtime);
}
});
const fastestOverall = Math.min(...allRuntimes);
const slowestOverall = Math.max(...allRuntimes);
// Identify iteration best/worst performing
const iterationRuntimes = [];
if (iteration.concatRuntime > 0) {
iterationRuntimes.push(iteration.concatRuntime);
}
if (iteration.pushRuntime > 0) {
iterationRuntimes.push(iteration.pushRuntime);
}
if (iteration.spreadRuntime > 0) {
iterationRuntimes.push(iteration.spreadRuntime);
}
if (iteration.spread2Runtime > 0) {
iterationRuntimes.push(iteration.spread2Runtime);
}
const fastestInIteration = Math.min(...iterationRuntimes);
const slowestInIteration = Math.max(...iterationRuntimes);
// Append iteration-specific classes
let classes = [];
if (value == fastestInIteration) {
classes.push('text-success');
} else if (value == slowestInIteration) {
classes.push('text-danger');
}
// Append test-wide classes
if (value == slowestOverall) {
classes.push('bg-danger');
classes.push('text-white');
} else if (value == fastestOverall) {
classes.push('bg-success');
classes.push('text-white');
}
return classes.join(' ');
}
}
};
const testApp = Vue.createApp(TestApp);
testApp.mount('#testApp');
</script>
</body>
</html>