Commit | Line | Data |
---|---|---|
8e5cf49d JB |
1 | const Benchmark = require('benny') |
2 | ||
3 | const testArray = [ | |
4 | 83, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, | |
5 | 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, | |
6 | 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, | |
7 | 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, | |
8 | 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, | |
9 | 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, 56, 32, | |
10 | 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, 234, 23, | |
11 | 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, 29, 2828, | |
12 | 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, 93, 27, | |
13 | 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, 36, 28, | |
14 | 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, 45, 93, 17, 28, 83, 62, 99, | |
15 | 36, 28 | |
16 | ] | |
17 | ||
18 | /** | |
19 | * | |
20 | * @param values | |
21 | */ | |
22 | function loopMax (values) { | |
23 | let max = -Infinity | |
24 | for (const value of values) { | |
25 | if (value > max) max = value | |
26 | } | |
27 | return max | |
28 | } | |
29 | ||
30 | /** | |
31 | * | |
32 | * @param values | |
33 | */ | |
34 | function reduceTernaryMax (values) { | |
35 | return values.reduce((a, b) => (a > b ? a : b), -Infinity) | |
36 | } | |
37 | ||
38 | /** | |
39 | * | |
40 | * @param values | |
41 | */ | |
42 | function reduceMathMax (values) { | |
43 | return values.reduce((a, b) => Math.max(a, b), -Infinity) | |
44 | } | |
45 | ||
46 | /** | |
47 | * | |
48 | * @param values | |
49 | */ | |
50 | function sortMax (values) { | |
51 | return values.sort((a, b) => b - a)[0] | |
52 | } | |
53 | ||
54 | Benchmark.suite( | |
55 | 'max', | |
56 | Benchmark.add('Math.max', () => { | |
57 | Math.max(...testArray) | |
58 | }), | |
59 | Benchmark.add('loopMax', () => { | |
60 | loopMax(testArray) | |
61 | }), | |
62 | Benchmark.add('reduceTernaryMax', () => { | |
63 | reduceTernaryMax(testArray) | |
64 | }), | |
65 | Benchmark.add('reduceMathMax', () => { | |
66 | reduceMathMax(testArray) | |
67 | }), | |
68 | Benchmark.add('sortMax', () => { | |
69 | sortMax(testArray) | |
70 | }), | |
71 | Benchmark.cycle(), | |
72 | Benchmark.complete(), | |
73 | Benchmark.save({ file: 'max', format: 'chart.html' }), | |
74 | Benchmark.save({ file: 'max', format: 'table.html' }) | |
75 | ) |