build(deps-dev): apply updates
[benchmarks-js.git] / min.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 loopMin (values) {
14 let minimum = Number.POSITIVE_INFINITY
15 for (const value of values) {
16 if (value < minimum) minimum = value
17 }
18 return minimum
19 }
20
21 /**
22 *
23 * @param values
24 * @returns
25 */
26 function reduceTernaryMin (values) {
27 return values.reduce(
28 (minimum, num) => (minimum < num ? minimum : num),
29 Number.POSITIVE_INFINITY
30 )
31 }
32
33 /**
34 *
35 * @param values
36 * @returns
37 */
38 function reduceMathMin (values) {
39 return values.reduce(
40 (minimum, num) => Math.min(minimum, num),
41 Number.POSITIVE_INFINITY
42 )
43 }
44
45 /**
46 *
47 * @param values
48 * @returns
49 */
50 function sortMin (values) {
51 return values.sort((a, b) => a - b)[0]
52 }
53
54 group(`Min from ${size} numbers`, () => {
55 bench('Math.min', () => {
56 Math.min(...testArray)
57 })
58 bench('loopMin', () => {
59 loopMin(testArray)
60 })
61 bench('reduceTernaryMin', () => {
62 reduceTernaryMin(testArray)
63 })
64 bench('reduceMathMin', () => {
65 reduceMathMin(testArray)
66 })
67 bench('sortMin', () => {
68 sortMin(testArray)
69 })
70 })
71
72 await run({
73 units: true
74 })