import { getRandomValues, randomBytes, randomInt } from 'node:crypto'
/**
- *
- * @param max
- * @param min
- * @returns
+ * Generates a random floating-point number between min and max.
+ * @param {number} max - The maximum value (inclusive).
+ * @param {number} min - The minimum value (inclusive).
+ * @returns {number} A random floating-point number between min and max.
*/
export function generateRandomFloat (max = Number.MAX_VALUE, min = 0) {
if (max < min) {
}
/**
- *
- * @param size
- * @param max
- * @param numberGenerator
- * @returns
+ * Generates an array of random numbers.
+ * @param {number} size - The size of the array to generate.
+ * @param {number} max - The maximum value for generated numbers.
+ * @param {(max: number, min?: number) => number} numberGenerator - The function to use for generating numbers.
+ * @returns {Array<number>} An array of random numbers.
*/
export function generateRandomNumberArray (
size,
}
/**
- *
- * @param sizeMax
- * @param numberMax
- * @param numberGenerator
- * @returns
+ * Generates a random object with numeric values.
+ * @param {number} sizeMax - The maximum size of the object.
+ * @param {number} numberMax - The maximum value for generated numbers.
+ * @param {(max: number, min?: number) => number} numberGenerator - The function to use for generating numbers.
+ * @returns {object} A random object with numeric properties.
*/
export function generateRandomObject (
sizeMax = 500,
/**
* Generate a cryptographically secure random number in the [0,1[ range
- * @returns
+ * @returns {number} A secure random number between 0 (inclusive) and 1 (exclusive).
*/
export function secureRandom () {
return randomBytes(4).readUInt32LE() / 0x100000000
/**
* Generate a cryptographically secure random number in the [0,1[ range
- * @returns
+ * @returns {number} A secure random number between 0 (inclusive) and 1 (exclusive).
*/
export function secureRandomWithRandomValues () {
return getRandomValues(new Uint32Array(1))[0] / 0x100000000
}
/**
- * @param ms
- * @returns
+ * Asynchronously sleep for a specified number of milliseconds.
+ * @param {number} ms - The number of milliseconds to sleep.
+ * @returns {Promise<void>} A promise that resolves after the specified delay.
*/
export async function sleep (ms) {
return await new Promise(resolve => setTimeout(resolve, ms))
const interval = 1000
/**
- * @param timeoutMs
- * @param intervalMs
+ * Busy wait implementation using divide and conquer strategy with sleep intervals.
+ * @param {number} timeoutMs - The total timeout in milliseconds.
+ * @param {number} intervalMs - The interval between checks in milliseconds.
+ * @returns {Promise<void>} A promise that resolves after the timeout.
*/
async function divideAndConquerTimeoutBusyWait (
timeoutMs,
}
/**
- * @param timeoutMs
+ * Dummy busy wait implementation using performance.now() in a tight loop.
+ * @param {number} timeoutMs - The timeout in milliseconds.
+ * @returns {void}
*/
function dummyTimeoutBusyWait (timeoutMs) {
const timeoutTimestampMs = performance.now() + timeoutMs
}
/**
- * @param timeoutMs
- * @param intervalMs
+ * Busy wait implementation using setInterval.
+ * @param {number} timeoutMs - The total timeout in milliseconds.
+ * @param {number} intervalMs - The interval between checks in milliseconds.
+ * @returns {Promise<void>} A promise that resolves after the timeout.
*/
async function setIntervalTimeoutBusyWait (timeoutMs, intervalMs = interval) {
await new Promise(resolve => {
}
/**
- * @param timeoutMs
- * @param intervalMs
+ * Busy wait implementation using repeated sleep calls.
+ * @param {number} timeoutMs - The total timeout in milliseconds.
+ * @param {number} intervalMs - The interval between sleep calls in milliseconds.
+ * @returns {Promise<void>} A promise that resolves after the timeout.
*/
async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) {
const timeoutTimestampMs = performance.now() + timeoutMs
const number = 30
/**
- * @param num
- * @returns
+ * Calculates the Fibonacci number at position num using a loop with array.
+ * @param {number} num - The position in the Fibonacci sequence.
+ * @returns {number} The Fibonacci number at the specified position.
*/
function fibonacciLoop (num) {
const fib = []
}
/**
- *
- * @param num
- * @returns
+ * Calculates the Fibonacci number at position num using a while loop.
+ * @param {number} num - The position in the Fibonacci sequence.
+ * @returns {number} The Fibonacci number at the specified position.
*/
function fibonacciLoopWhile (num) {
let current = 1
}
/**
- * @param num
- * @returns
+ * Calculates the Fibonacci number at position num using recursion.
+ * @param {number} num - The position in the Fibonacci sequence.
+ * @returns {number} The Fibonacci number at the specified position.
*/
function fibonacciRecursion (num) {
if (num <= 1) return num
}
/**
- * @param num
- * @param memo
- * @returns
+ * Calculates the Fibonacci number at position num using recursion with memoization.
+ * @param {number} num - The position in the Fibonacci sequence.
+ * @param {{[key: number]: number}} [memo] - The memoization object to cache results.
+ * @returns {number} The Fibonacci number at the specified position.
*/
function fibonacciRecursionMemoization (num, memo) {
memo = memo || {}
const testArray = generateRandomNumberArray(size)
/**
- *
- * @param values
- * @returns
+ * Finds the maximum value in an array using a loop.
+ * @param {Array<number>} values - The array of numbers to search.
+ * @returns {number} The maximum value in the array.
*/
function loopMax (values) {
let maximum = Number.NEGATIVE_INFINITY
}
/**
- *
- * @param values
- * @returns
+ * Finds the maximum value in an array using reduce with Math.max.
+ * @param {Array<number>} values - The array of numbers to search.
+ * @returns {number} The maximum value in the array.
*/
function reduceMathMax (values) {
return values.reduce(
}
/**
- *
- * @param values
- * @returns
+ * Finds the maximum value in an array using reduce with ternary operator.
+ * @param {Array<number>} values - The array of numbers to search.
+ * @returns {number} The maximum value in the array.
*/
function reduceTernaryMax (values) {
return values.reduce(
}
/**
- *
- * @param values
- * @returns
+ * Finds the maximum value in an array by sorting.
+ * @param {Array<number>} values - The array of numbers to search.
+ * @returns {number} The maximum value in the array.
*/
function sortMax (values) {
return values.sort((a, b) => b - a)[0]
const testArray = generateRandomNumberArray(size)
/**
- *
- * @param values
- * @returns
+ * Finds the minimum value in an array using a loop.
+ * @param {Array<number>} values - The array of numbers to search.
+ * @returns {number} The minimum value in the array.
*/
function loopMin (values) {
let minimum = Number.POSITIVE_INFINITY
}
/**
- *
- * @param values
- * @returns
+ * Finds the minimum value in an array using reduce with Math.min.
+ * @param {Array<number>} values - The array of numbers to search.
+ * @returns {number} The minimum value in the array.
*/
function reduceMathMin (values) {
return values.reduce(
}
/**
- *
- * @param values
- * @returns
+ * Finds the minimum value in an array using reduce with ternary operator.
+ * @param {Array<number>} values - The array of numbers to search.
+ * @returns {number} The minimum value in the array.
*/
function reduceTernaryMin (values) {
return values.reduce(
}
/**
- *
- * @param values
- * @returns
+ * Finds the minimum value in an array by sorting.
+ * @param {Array<number>} values - The array of numbers to search.
+ * @returns {number} The minimum value in the array.
*/
function sortMin (values) {
return values.sort((a, b) => a - b)[0]
import { bench, group, run } from 'tatami-ng'
/**
- * @param numberOfWorkers
- * @param maxNumberOfTasksPerWorker
- * @returns
+ * Generates a random tasks map for benchmarking.
+ * @param {number} numberOfWorkers - The number of workers.
+ * @param {number} maxNumberOfTasksPerWorker - The maximum number of tasks per worker.
+ * @returns {Map} A map with worker IDs as keys and task counts as values.
*/
function generateRandomTasksMap (
numberOfWorkers,
const tasksMap = generateRandomTasksMap(60, 20)
/**
- * @param tasksMap
- * @returns
+ * Selects the task with minimum value using array sort.
+ * @param {Map} tasksMap - The map of tasks to search.
+ * @returns {Array} The task entry with the minimum value.
*/
function arraySortSelect (tasksMap) {
const tasksArray = Array.from(tasksMap)
}
/**
- * @param tasksMap
- * @returns
+ * Selects the task with minimum value using a loop.
+ * @param {Map} tasksMap - The map of tasks to search.
+ * @returns {Array|number} The task entry with the minimum value.
*/
function loopSelect (tasksMap) {
let minKey
}
/**
- * @param array
- * @param leftIndex
- * @param rightIndex
- * @param pivotIndex
- * @param compare
- * @returns
+ * Partitions an array for quickselect algorithm.
+ * @template T
+ * @param {Array<T>} array - The array to partition.
+ * @param {number} leftIndex - The left index of the partition range.
+ * @param {number} rightIndex - The right index of the partition range.
+ * @param {number} pivotIndex - The index of the pivot element.
+ * @param {(a: T, b: T) => boolean} compare - The comparison function.
+ * @returns {number} The final position of the pivot element.
*/
function partition (
array,
}
/**
- * @param tasksMap
- * @returns
+ * Selects task using quickselect with loop implementation.
+ * @param {Map} tasksMap - The map of tasks to search.
+ * @returns {Array} The task entry with the minimum value.
*/
function quickSelectLoop (tasksMap) {
const tasksArray = Array.from(tasksMap)
}
/**
- * @param tasksMap
- * @returns
+ * Selects task using quickselect with loop and random pivot.
+ * @param {Map} tasksMap - The map of tasks to search.
+ * @returns {Array} The task entry with the minimum value.
*/
function quickSelectLoopRandomPivot (tasksMap) {
const tasksArray = Array.from(tasksMap)
}
/**
- * @param tasksMap
- * @returns
+ * Selects task using quickselect with recursion.
+ * @param {Map} tasksMap - The map of tasks to search.
+ * @returns {Array} The task entry with the minimum value.
*/
function quickSelectRecursion (tasksMap) {
const tasksArray = Array.from(tasksMap)
}
/**
- * @param tasksMap
- * @returns
+ * Selects task using quickselect with recursion and random pivot.
+ * @param {Map} tasksMap - The map of tasks to search.
+ * @returns {Array} The task entry with the minimum value.
*/
function quickSelectRecursionRandomPivot (tasksMap) {
const tasksArray = Array.from(tasksMap)
}
/**
- * @param array
- * @param k
- * @param leftIndex
- * @param rightIndex
- * @param compare
- * @param pivotIndexSelect
- * @returns
+ * Quickselect algorithm using loop implementation.
+ * @template T
+ * @param {Array<T>} array - The array to search.
+ * @param {number} k - The k-th element to find.
+ * @param {number} leftIndex - The left boundary index.
+ * @param {number} rightIndex - The right boundary index.
+ * @param {(a: T, b: T) => boolean} compare - The comparison function.
+ * @param {(leftIndex: number, rightIndex: number) => number} pivotIndexSelect - The pivot selection function.
+ * @returns {T} The k-th element.
*/
function selectLoop (
array,
}
/**
- * @param array
- * @param k
- * @param leftIndex
- * @param rightIndex
- * @param compare
- * @param pivotIndexSelect
- * @returns
+ * Quickselect algorithm using recursion.
+ * @template T
+ * @param {Array<T>} array - The array to search.
+ * @param {number} k - The k-th element to find.
+ * @param {number} leftIndex - The left boundary index.
+ * @param {number} rightIndex - The right boundary index.
+ * @param {(a: T, b: T) => boolean} compare - The comparison function.
+ * @param {(leftIndex: number, rightIndex: number) => number} pivotIndexSelect - The pivot selection function.
+ * @returns {T} The k-th element.
*/
function selectRecursion (
array,
}
/**
- * @param array
- * @param index1
- * @param index2
+ * Swaps two elements in an array.
+ * @param {Array} array - The array containing elements to swap.
+ * @param {number} index1 - The index of the first element.
+ * @param {number} index2 - The index of the second element.
+ * @returns {void}
*/
function swap (array, index1, index2) {
const tmp = array[index1]
const maximum = 281474976710655
/**
- * @param max
- * @param min
- * @returns
+ * Generates a random integer between min and max using Math.random.
+ * @param {number} max - The maximum value (inclusive).
+ * @param {number} min - The minimum value (inclusive).
+ * @returns {number} A random integer between min and max.
*/
function getRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
if (max < min || max < 0 || min < 0) {
}
/**
- * @param max
- * @param min
- * @returns
+ * Generates a cryptographically secure random integer using secureRandom.
+ * @param {number} max - The maximum value (inclusive).
+ * @param {number} min - The minimum value (inclusive).
+ * @returns {number} A secure random integer between min and max.
*/
function getSecureRandomInteger (max = Number.MAX_SAFE_INTEGER, min = 0) {
if (max < min || max < 0 || min < 0) {
}
/**
- * @param max
- * @param min
- * @returns
+ * Generates a cryptographically secure random integer using getRandomValues.
+ * @param {number} max - The maximum value (inclusive).
+ * @param {number} min - The minimum value (inclusive).
+ * @returns {number} A secure random integer between min and max.
*/
function getSecureRandomIntegerWithRandomValues (
max = Number.MAX_SAFE_INTEGER,