1 const Benchmark
= require('benny')
2 const { generateRandomNumberArray
} = require('./benchmark-utils')
4 const testArray
= generateRandomNumberArray(10000)
11 function loopMax (values
) {
13 for (const value
of values
) {
14 if (value
> max
) max
= value
24 function reduceTernaryMax (values
) {
25 return values
.reduce((a
, b
) => (a
> b
? a
: b
), -Infinity
)
33 function reduceMathMax (values
) {
34 return values
.reduce((a
, b
) => Math
.max(a
, b
), -Infinity
)
42 function sortMax (values
) {
43 return values
.sort((a
, b
) => b
- a
)[0]
48 Benchmark
.add('Math.max', () => {
49 Math
.max(...testArray
)
51 Benchmark
.add('loopMax', () => {
54 Benchmark
.add('reduceTernaryMax', () => {
55 reduceTernaryMax(testArray
)
57 Benchmark
.add('reduceMath.max', () => {
58 reduceMathMax(testArray
)
60 Benchmark
.add('sortMax', () => {
65 Benchmark
.save({ file
: 'max', format
: 'json', details
: true }),
66 Benchmark
.save({ file
: 'max', format
: 'chart.html', details
: true }),
67 Benchmark
.save({ file
: 'max', format
: 'table.html', details
: true })