Commit | Line | Data |
---|---|---|
4676a95c | 1 | import { bench, group, run } from 'tatami-ng' |
0c01f51c | 2 | |
95d31631 | 3 | import { generateRandomNumberArray } from './benchmark-utils.mjs' |
8e5cf49d | 4 | |
bb578730 JB |
5 | const size = 10000 |
6 | const testArray = generateRandomNumberArray(size) | |
8e5cf49d JB |
7 | |
8 | /** | |
9 | * | |
10 | * @param values | |
47006ddf | 11 | * @returns |
8e5cf49d JB |
12 | */ |
13 | function loopMax (values) { | |
ae56b43d | 14 | let maximum = Number.NEGATIVE_INFINITY |
8e5cf49d | 15 | for (const value of values) { |
528db716 | 16 | if (value > maximum) maximum = value |
8e5cf49d | 17 | } |
528db716 | 18 | return maximum |
8e5cf49d JB |
19 | } |
20 | ||
21 | /** | |
22 | * | |
23 | * @param values | |
47006ddf | 24 | * @returns |
8e5cf49d JB |
25 | */ |
26 | function reduceTernaryMax (values) { | |
528db716 JB |
27 | return values.reduce( |
28 | (maximum, num) => (maximum > num ? maximum : num), | |
ae56b43d | 29 | Number.NEGATIVE_INFINITY |
528db716 | 30 | ) |
8e5cf49d JB |
31 | } |
32 | ||
33 | /** | |
34 | * | |
35 | * @param values | |
47006ddf | 36 | * @returns |
8e5cf49d JB |
37 | */ |
38 | function reduceMathMax (values) { | |
ae56b43d JB |
39 | return values.reduce( |
40 | (maximum, num) => Math.max(maximum, num), | |
41 | Number.NEGATIVE_INFINITY | |
42 | ) | |
8e5cf49d JB |
43 | } |
44 | ||
45 | /** | |
46 | * | |
47 | * @param values | |
47006ddf | 48 | * @returns |
8e5cf49d JB |
49 | */ |
50 | function sortMax (values) { | |
51 | return values.sort((a, b) => b - a)[0] | |
52 | } | |
53 | ||
ab9a08f3 JB |
54 | group(`Max from ${size} numbers`, () => { |
55 | bench('Math.max', () => { | |
8e5cf49d | 56 | Math.max(...testArray) |
ab9a08f3 JB |
57 | }) |
58 | bench('loopMax', () => { | |
8e5cf49d | 59 | loopMax(testArray) |
ab9a08f3 JB |
60 | }) |
61 | bench('reduceTernaryMax', () => { | |
8e5cf49d | 62 | reduceTernaryMax(testArray) |
ab9a08f3 JB |
63 | }) |
64 | bench('reduceMathMax', () => { | |
8e5cf49d | 65 | reduceMathMax(testArray) |
ab9a08f3 JB |
66 | }) |
67 | bench('sortMax', () => { | |
8e5cf49d | 68 | sortMax(testArray) |
ab9a08f3 JB |
69 | }) |
70 | }) | |
71 | ||
72 | await run({ | |
ebf80fe4 | 73 | units: true, |
ab9a08f3 | 74 | }) |