1 import Benchmark from 'benny'
2 import { generateRandomNumberArray } from './benchmark-utils.mjs'
5 const testArray = generateRandomNumberArray(size)
12 function loopMax (values) {
13 let maximum = -Infinity
14 for (const value of values) {
15 if (value > maximum) maximum = value
25 function reduceTernaryMax (values) {
27 (maximum, num) => (maximum > num ? maximum : num),
37 function reduceMathMax (values) {
38 return values.reduce((maximum, num) => Math.max(maximum, num), -Infinity)
46 function sortMax (values) {
47 return values.sort((a, b) => b - a)[0]
51 `Max from ${size} numbers`,
52 Benchmark.add('Math.max', () => {
53 Math.max(...testArray)
55 Benchmark.add('loopMax', () => {
58 Benchmark.add('reduceTernaryMax', () => {
59 reduceTernaryMax(testArray)
61 Benchmark.add('reduceMath.max', () => {
62 reduceMathMax(testArray)
64 Benchmark.add('sortMax', () => {
69 Benchmark.save({ file: 'max', format: 'json', details: true }),
70 Benchmark.save({ file: 'max', format: 'chart.html', details: true }),
71 Benchmark.save({ file: 'max', format: 'table.html', details: true })