1 const Benchmark
= require('benny')
2 const { generateRandomNumberArray
} = require('./benchmark-utils')
5 const testArray
= generateRandomNumberArray(size
)
12 function loopMax (values
) {
14 for (const value
of values
) {
15 if (value
> max
) max
= value
25 function reduceTernaryMax (values
) {
26 return values
.reduce((a
, b
) => (a
> b
? a
: b
), -Infinity
)
34 function reduceMathMax (values
) {
35 return values
.reduce((a
, b
) => Math
.max(a
, b
), -Infinity
)
43 function sortMax (values
) {
44 return values
.sort((a
, b
) => b
- a
)[0]
48 `Max from ${size} numbers`,
49 Benchmark
.add('Math.max', () => {
50 Math
.max(...testArray
)
52 Benchmark
.add('loopMax', () => {
55 Benchmark
.add('reduceTernaryMax', () => {
56 reduceTernaryMax(testArray
)
58 Benchmark
.add('reduceMath.max', () => {
59 reduceMathMax(testArray
)
61 Benchmark
.add('sortMax', () => {
66 Benchmark
.save({ file
: 'max', format
: 'json', details
: true }),
67 Benchmark
.save({ file
: 'max', format
: 'chart.html', details
: true }),
68 Benchmark
.save({ file
: 'max', format
: 'table.html', details
: true })