Commit | Line | Data |
---|---|---|
f522d7b9 | 1 | import Benchmark from 'benny' |
95d31631 | 2 | import { generateRandomNumberArray } from './benchmark-utils.mjs' |
8e5cf49d | 3 | |
bb578730 JB |
4 | const size = 10000 |
5 | const testArray = generateRandomNumberArray(size) | |
8e5cf49d JB |
6 | |
7 | /** | |
8 | * | |
9 | * @param values | |
47006ddf | 10 | * @returns |
8e5cf49d JB |
11 | */ |
12 | function loopMax (values) { | |
528db716 | 13 | let maximum = -Infinity |
8e5cf49d | 14 | for (const value of values) { |
528db716 | 15 | if (value > maximum) maximum = value |
8e5cf49d | 16 | } |
528db716 | 17 | return maximum |
8e5cf49d JB |
18 | } |
19 | ||
20 | /** | |
21 | * | |
22 | * @param values | |
47006ddf | 23 | * @returns |
8e5cf49d JB |
24 | */ |
25 | function reduceTernaryMax (values) { | |
528db716 JB |
26 | return values.reduce( |
27 | (maximum, num) => (maximum > num ? maximum : num), | |
28 | -Infinity | |
29 | ) | |
8e5cf49d JB |
30 | } |
31 | ||
32 | /** | |
33 | * | |
34 | * @param values | |
47006ddf | 35 | * @returns |
8e5cf49d JB |
36 | */ |
37 | function reduceMathMax (values) { | |
528db716 | 38 | return values.reduce((maximum, num) => Math.max(maximum, num), -Infinity) |
8e5cf49d JB |
39 | } |
40 | ||
41 | /** | |
42 | * | |
43 | * @param values | |
47006ddf | 44 | * @returns |
8e5cf49d JB |
45 | */ |
46 | function sortMax (values) { | |
47 | return values.sort((a, b) => b - a)[0] | |
48 | } | |
49 | ||
50 | Benchmark.suite( | |
bb578730 | 51 | `Max from ${size} numbers`, |
8e5cf49d JB |
52 | Benchmark.add('Math.max', () => { |
53 | Math.max(...testArray) | |
54 | }), | |
55 | Benchmark.add('loopMax', () => { | |
56 | loopMax(testArray) | |
57 | }), | |
58 | Benchmark.add('reduceTernaryMax', () => { | |
59 | reduceTernaryMax(testArray) | |
60 | }), | |
fb341cfe | 61 | Benchmark.add('reduceMath.max', () => { |
8e5cf49d JB |
62 | reduceMathMax(testArray) |
63 | }), | |
64 | Benchmark.add('sortMax', () => { | |
65 | sortMax(testArray) | |
66 | }), | |
67 | Benchmark.cycle(), | |
68 | Benchmark.complete(), | |
fb341cfe JB |
69 | Benchmark.save({ file: 'max', format: 'json', details: true }), |
70 | Benchmark.save({ file: 'max', format: 'chart.html', details: true }), | |
71 | Benchmark.save({ file: 'max', format: 'table.html', details: true }) | |
4aa2893a | 72 | ).catch(console.error) |