Add empty object helper implementations benchmark
[benchmarks-js.git] / max.js
1 const Benchmark = require('benny')
2 const { generateRandomNumberArray } = require('./benchmark-utils')
3
4 const testArray = generateRandomNumberArray(10000)
5
6 /**
7 *
8 * @param values
9 * @returns
10 */
11 function loopMax (values) {
12 let max = -Infinity
13 for (const value of values) {
14 if (value > max) max = value
15 }
16 return max
17 }
18
19 /**
20 *
21 * @param values
22 * @returns
23 */
24 function reduceTernaryMax (values) {
25 return values.reduce((a, b) => (a > b ? a : b), -Infinity)
26 }
27
28 /**
29 *
30 * @param values
31 * @returns
32 */
33 function reduceMathMax (values) {
34 return values.reduce((a, b) => Math.max(a, b), -Infinity)
35 }
36
37 /**
38 *
39 * @param values
40 * @returns
41 */
42 function sortMax (values) {
43 return values.sort((a, b) => b - a)[0]
44 }
45
46 Benchmark.suite(
47 'max',
48 Benchmark.add('Math.max', () => {
49 Math.max(...testArray)
50 }),
51 Benchmark.add('loopMax', () => {
52 loopMax(testArray)
53 }),
54 Benchmark.add('reduceTernaryMax', () => {
55 reduceTernaryMax(testArray)
56 }),
57 Benchmark.add('reduceMath.max', () => {
58 reduceMathMax(testArray)
59 }),
60 Benchmark.add('sortMax', () => {
61 sortMax(testArray)
62 }),
63 Benchmark.cycle(),
64 Benchmark.complete(),
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 })
68 )