Add empty object helper implementations benchmark
[benchmarks-js.git] / max.js
CommitLineData
8e5cf49d 1const Benchmark = require('benny')
feb629fe 2const { generateRandomNumberArray } = require('./benchmark-utils')
8e5cf49d 3
feb629fe 4const testArray = generateRandomNumberArray(10000)
8e5cf49d
JB
5
6/**
7 *
8 * @param values
47006ddf 9 * @returns
8e5cf49d
JB
10 */
11function 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
47006ddf 22 * @returns
8e5cf49d
JB
23 */
24function reduceTernaryMax (values) {
25 return values.reduce((a, b) => (a > b ? a : b), -Infinity)
26}
27
28/**
29 *
30 * @param values
47006ddf 31 * @returns
8e5cf49d
JB
32 */
33function reduceMathMax (values) {
34 return values.reduce((a, b) => Math.max(a, b), -Infinity)
35}
36
37/**
38 *
39 * @param values
47006ddf 40 * @returns
8e5cf49d
JB
41 */
42function sortMax (values) {
43 return values.sort((a, b) => b - a)[0]
44}
45
46Benchmark.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 }),
fb341cfe 57 Benchmark.add('reduceMath.max', () => {
8e5cf49d
JB
58 reduceMathMax(testArray)
59 }),
60 Benchmark.add('sortMax', () => {
61 sortMax(testArray)
62 }),
63 Benchmark.cycle(),
64 Benchmark.complete(),
fb341cfe
JB
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 })
8e5cf49d 68)