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 = -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 -Infinity
30 )
31 }
32
33 /**
34 *
35 * @param values
36 * @returns
37 */
38 function reduceMathMax (values) {
39 return values.reduce((maximum, num) => Math.max(maximum, num), -Infinity)
40 }
41
42 /**
43 *
44 * @param values
45 * @returns
46 */
47 function sortMax (values) {
48 return values.sort((a, b) => b - a)[0]
49 }
50
51 group(`Max from ${size} numbers`, () => {
52 bench('Math.max', () => {
53 Math.max(...testArray)
54 })
55 bench('loopMax', () => {
56 loopMax(testArray)
57 })
58 bench('reduceTernaryMax', () => {
59 reduceTernaryMax(testArray)
60 })
61 bench('reduceMathMax', () => {
62 reduceMathMax(testArray)
63 })
64 bench('sortMax', () => {
65 sortMax(testArray)
66 })
67 })
68
69 await run({
70 units: true
71 })