X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=max.mjs;h=8675f8292dfe81ed07f11ce5f84e5bcd8c7a2280;hb=ae56b43d49a0cb5a88a953a7f6ef42f02e4a12d9;hp=8914520e21c75b7e2ee5d50b41ea80a1f310b142;hpb=528db71629bde2fbb2332fadc1011df3a61dd7c7;p=benchmarks-js.git diff --git a/max.mjs b/max.mjs index 8914520..8675f82 100644 --- a/max.mjs +++ b/max.mjs @@ -1,4 +1,5 @@ -import Benchmark from 'benny' +import { bench, group, run } from 'tatami-ng' + import { generateRandomNumberArray } from './benchmark-utils.mjs' const size = 10000 @@ -10,7 +11,7 @@ const testArray = generateRandomNumberArray(size) * @returns */ function loopMax (values) { - let maximum = -Infinity + let maximum = Number.NEGATIVE_INFINITY for (const value of values) { if (value > maximum) maximum = value } @@ -25,7 +26,7 @@ function loopMax (values) { function reduceTernaryMax (values) { return values.reduce( (maximum, num) => (maximum > num ? maximum : num), - -Infinity + Number.NEGATIVE_INFINITY ) } @@ -35,7 +36,10 @@ function reduceTernaryMax (values) { * @returns */ function reduceMathMax (values) { - return values.reduce((maximum, num) => Math.max(maximum, num), -Infinity) + return values.reduce( + (maximum, num) => Math.max(maximum, num), + Number.NEGATIVE_INFINITY + ) } /** @@ -47,28 +51,24 @@ function sortMax (values) { return values.sort((a, b) => b - a)[0] } -Benchmark.suite( - `Max from ${size} numbers`, - Benchmark.add('Math.max', () => { +group(`Max from ${size} numbers`, () => { + bench('Math.max', () => { Math.max(...testArray) - }), - Benchmark.add('loopMax', () => { + }) + bench('loopMax', () => { loopMax(testArray) - }), - Benchmark.add('reduceTernaryMax', () => { + }) + bench('reduceTernaryMax', () => { reduceTernaryMax(testArray) - }), - Benchmark.add('reduceMath.max', () => { + }) + bench('reduceMathMax', () => { reduceMathMax(testArray) - }), - Benchmark.add('sortMax', () => { + }) + bench('sortMax', () => { sortMax(testArray) - }), - Benchmark.cycle(), - Benchmark.complete(), - Benchmark.save({ file: 'max', format: 'json', details: true }), - Benchmark.save({ file: 'max', format: 'chart.html', details: true }), - Benchmark.save({ file: 'max', format: 'table.html', details: true }) -).catch((err) => { - console.error(err) + }) +}) + +await run({ + units: true })