build(deps-dev): apply updates
[benchmarks-js.git] / max.mjs
CommitLineData
4676a95c 1import { bench, group, run } from 'tatami-ng'
0c01f51c 2
95d31631 3import { generateRandomNumberArray } from './benchmark-utils.mjs'
8e5cf49d 4
bb578730
JB
5const size = 10000
6const testArray = generateRandomNumberArray(size)
8e5cf49d
JB
7
8/**
9 *
10 * @param values
47006ddf 11 * @returns
8e5cf49d
JB
12 */
13function 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 */
26function 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 */
38function 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 */
50function sortMax (values) {
51 return values.sort((a, b) => b - a)[0]
52}
53
ab9a08f3
JB
54group(`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
72await run({
ebf80fe4 73 units: true,
ab9a08f3 74})