return Math.floor(Math.random() * max + 1)
}
+/**
+ * @param ms
+ */
+async function sleep (ms) {
+ return new Promise(resolve => setTimeout(resolve, ms))
+}
+
const LIST_FORMATTER = new Intl.ListFormat('en-US', {
style: 'long',
type: 'conjunction'
})
-module.exports = { generateRandomInteger, LIST_FORMATTER }
+module.exports = { generateRandomInteger, sleep, LIST_FORMATTER }
const Benchmark = require('benchmark')
-const { LIST_FORMATTER } = require('./benchmark-utils')
+const { LIST_FORMATTER, sleep } = require('./benchmark-utils')
const suite = new Benchmark.Suite()
* @param timeoutMs
*/
function dummyTimeoutBusyWait (timeoutMs) {
- const timeoutDateMs = Date.now() + timeoutMs
- do {} while (Date.now() < timeoutDateMs)
+ const timeoutTimestampMs = Date.now() + timeoutMs
+ do {} while (Date.now() < timeoutTimestampMs)
+}
+
+/**
+ * @param timeoutMs
+ * @param delayMs
+ */
+async function divideAndConquerTimeoutBusyWait (timeoutMs, delayMs = 200) {
+ const tries = Math.round(timeoutMs / delayMs)
+ let count = 0
+ do {
+ count++
+ await sleep(delayMs)
+ } while (count <= tries)
}
/**
.add('dummyTimeoutBusyWait', function () {
dummyTimeoutBusyWait(timeout)
})
+ .add('divideAndConquerTimeoutBusyWait', async function () {
+ await divideAndConquerTimeoutBusyWait(timeout)
+ })
.add('setIntervalTimeoutBusyWait', function () {
setIntervalTimeoutBusyWait(timeout)
})
--- /dev/null
+const Benchmark = require('benchmark')
+const { LIST_FORMATTER } = require('./benchmark-utils')
+
+const suite = new Benchmark.Suite()
+
+const number = 30
+
+/**
+ * @param num
+ */
+function fibonacciLoop (num) {
+ let a = 1
+ let b = 0
+ let temp
+
+ while (num >= 0) {
+ temp = a
+ a = a + b
+ b = temp
+ num--
+ }
+
+ return b
+}
+
+/**
+ * @param num
+ */
+function fibonacciRecursion (num) {
+ if (num <= 1) return 1
+
+ return fibonacciRecursion(num - 1) + fibonacciRecursion(num - 2)
+}
+
+/**
+ * @param num
+ * @param memo
+ */
+function fibonacciRecursionMemoization (num, memo) {
+ memo = memo || {}
+
+ if (memo[num]) return memo[num]
+ if (num <= 1) return 1
+
+ return (memo[num] =
+ fibonacciRecursionMemoization(num - 1, memo) +
+ fibonacciRecursionMemoization(num - 2, memo))
+}
+
+suite
+ .add('fibonacciLoop', function () {
+ fibonacciLoop(number)
+ })
+ .add('fibonacciRecursion', function () {
+ fibonacciRecursion(number)
+ })
+ .add('fibonacciRecursionMemoization', function () {
+ fibonacciRecursionMemoization(number)
+ })
+ .on('cycle', function (event) {
+ console.log(event.target.toString())
+ })
+ .on('complete', function () {
+ console.log(
+ 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
+ )
+ // eslint-disable-next-line no-process-exit
+ process.exit()
+ })
+ .run()
"benchmark:busy-wait": "node busy-wait.js",
"benchmark:quick-select": "node quick-select.js",
"benchmark:promise-handling": "node promise-handling.js",
+ "benchmark:fibonacci": "node fibonacci.js",
"format": "prettier --loglevel silent --write .; prettierx --write .",
"lint": "eslint .",
"lint:fix": "eslint . --fix",