1 import Benchmark from 'benny'
3 import { generateRandomNumberArray } from './benchmark-utils.mjs'
6 const testArray = generateRandomNumberArray(size)
13 function loopMax (values) {
14 let maximum = -Infinity
15 for (const value of values) {
16 if (value > maximum) maximum = value
26 function reduceTernaryMax (values) {
28 (maximum, num) => (maximum > num ? maximum : num),
38 function reduceMathMax (values) {
39 return values.reduce((maximum, num) => Math.max(maximum, num), -Infinity)
47 function sortMax (values) {
48 return values.sort((a, b) => b - a)[0]
52 `Max from ${size} numbers`,
53 Benchmark.add('Math.max', () => {
54 Math.max(...testArray)
56 Benchmark.add('loopMax', () => {
59 Benchmark.add('reduceTernaryMax', () => {
60 reduceTernaryMax(testArray)
62 Benchmark.add('reduceMath.max', () => {
63 reduceMathMax(testArray)
65 Benchmark.add('sortMax', () => {
70 Benchmark.save({ file: 'max', format: 'json', details: true }),
71 Benchmark.save({ file: 'max', format: 'chart.html', details: true }),
72 Benchmark.save({ file: 'max', format: 'table.html', details: true })
73 ).catch(console.error)