1 const Benchmark
= require('benny')
2 const { generateRandomNumberArray
} = require('./benchmark-utils')
4 const testArray
= generateRandomNumberArray(10000)
10 function loopMax (values
) {
12 for (const value
of values
) {
13 if (value
> max
) max
= value
22 function reduceTernaryMax (values
) {
23 return values
.reduce((a
, b
) => (a
> b
? a
: b
), -Infinity
)
30 function reduceMathMax (values
) {
31 return values
.reduce((a
, b
) => Math
.max(a
, b
), -Infinity
)
38 function sortMax (values
) {
39 return values
.sort((a
, b
) => b
- a
)[0]
44 Benchmark
.add('Math.max', () => {
45 Math
.max(...testArray
)
47 Benchmark
.add('loopMax', () => {
50 Benchmark
.add('reduceTernaryMax', () => {
51 reduceTernaryMax(testArray
)
53 Benchmark
.add('reduceMath.max', () => {
54 reduceMathMax(testArray
)
56 Benchmark
.add('sortMax', () => {
61 Benchmark
.save({ file
: 'max', format
: 'json', details
: true }),
62 Benchmark
.save({ file
: 'max', format
: 'chart.html', details
: true }),
63 Benchmark
.save({ file
: 'max', format
: 'table.html', details
: true })