X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=min.mjs;h=858b1eb3fc9c7e5d4b4cc456f1925b9f9205ce6b;hb=HEAD;hp=3ea46c4be780e94d0dbf9db1d09b6e4b932cf9eb;hpb=a6affe1656dc6a175c87eb5ad5ac4706243d56cf;p=benchmarks-js.git diff --git a/min.mjs b/min.mjs index 3ea46c4..44beb09 100644 --- a/min.mjs +++ b/min.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,11 +11,11 @@ const testArray = generateRandomNumberArray(size) * @returns */ function loopMin (values) { - let min = Infinity + let minimum = Number.POSITIVE_INFINITY for (const value of values) { - if (value < min) min = value + if (value < minimum) minimum = value } - return min + return minimum } /** @@ -23,7 +24,10 @@ function loopMin (values) { * @returns */ function reduceTernaryMin (values) { - return values.reduce((a, b) => (a < b ? a : b), Infinity) + return values.reduce( + (minimum, num) => (minimum < num ? minimum : num), + Number.POSITIVE_INFINITY + ) } /** @@ -32,7 +36,10 @@ function reduceTernaryMin (values) { * @returns */ function reduceMathMin (values) { - return values.reduce((a, b) => Math.min(a, b), Infinity) + return values.reduce( + (minimum, num) => Math.min(minimum, num), + Number.POSITIVE_INFINITY + ) } /** @@ -44,28 +51,24 @@ function sortMin (values) { return values.sort((a, b) => a - b)[0] } -Benchmark.suite( - `Min from ${size} numbers`, - Benchmark.add('Math.min', () => { +group(`Min from ${size} numbers`, () => { + bench('Math.min', () => { Math.min(...testArray) - }), - Benchmark.add('loopMin', () => { + }) + bench('loopMin', () => { loopMin(testArray) - }), - Benchmark.add('reduceTernaryMin', () => { + }) + bench('reduceTernaryMin', () => { reduceTernaryMin(testArray) - }), - Benchmark.add('reduceMath.min', () => { + }) + bench('reduceMathMin', () => { reduceMathMin(testArray) - }), - Benchmark.add('sortMin', () => { + }) + bench('sortMin', () => { sortMin(testArray) - }), - Benchmark.cycle(), - Benchmark.complete(), - Benchmark.save({ file: 'min', format: 'json', details: true }), - Benchmark.save({ file: 'min', format: 'chart.html', details: true }), - Benchmark.save({ file: 'min', format: 'table.html', details: true }) -).catch((err) => { - console.error(err) + }) +}) + +await run({ + units: true, })