X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=benchmarks%2Finternal%2Fbenchmark-utils.js;h=7cb4e54f500283d5c5da6e69b743f5b851ba344e;hb=7d82d90ea31689b7235a9282ccd5a2439483bc9f;hp=878c2cae4b34bcd77e8c24cc96c96b109c290c43;hpb=292ad316a2815762f2e4a822383f1aef5ae49774;p=poolifier.git diff --git a/benchmarks/internal/benchmark-utils.js b/benchmarks/internal/benchmark-utils.js index 878c2cae..7cb4e54f 100644 --- a/benchmarks/internal/benchmark-utils.js +++ b/benchmarks/internal/benchmark-utils.js @@ -11,16 +11,55 @@ async function runPoolifierTest (pool, { tasks, workerData }) { } return null }) - .catch(err => console.error(err)) + .catch(err => { + console.error(err) + return reject(err) + }) } }) } +function jsonIntegerSerialization (n) { + for (let i = 0; i < n; i++) { + const o = { + a: i + } + JSON.stringify(o) + } +} + function generateRandomInteger (max, min = 0) { + max = Math.floor(max) if (min) { - return Math.floor(Math.random() * (max - min + 1) + min) + min = Math.ceil(min) + return Math.floor(Math.random() * (max - min + 1)) + min + } + return Math.floor(Math.random() * (max + 1)) +} + +/** + * Intentionally inefficient implementation. + * + * @param {number} n - The number of fibonacci numbers to generate. + * @returns {number} - The nth fibonacci number. + */ +function fibonacci (n) { + if (n <= 1) return 1 + return fibonacci(n - 1) + fibonacci(n - 2) +} + +/** + * Intentionally inefficient implementation. + * + * @param {number} n - The number to calculate the factorial of. + * @returns {number} - The factorial of n. + */ +function factorial (n) { + if (n === 0) { + return 1 + } else { + return factorial(n - 1) * n } - return Math.floor(Math.random() * max + 1) } const LIST_FORMATTER = new Intl.ListFormat('en-US', { @@ -28,4 +67,11 @@ const LIST_FORMATTER = new Intl.ListFormat('en-US', { type: 'conjunction' }) -module.exports = { generateRandomInteger, LIST_FORMATTER, runPoolifierTest } +module.exports = { + runPoolifierTest, + jsonIntegerSerialization, + generateRandomInteger, + fibonacci, + factorial, + LIST_FORMATTER +}