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