Skip to content

min

Returns the minimum value in an array using a loop instead of spread, avoiding the call stack overflow that occurs with Math.min(...array) for very large arrays (> ~65 000 elements). null and undefined are treated as empty arrays and return undefined.

Available since v2.0.2

import { min } from '@helpers4/array';
// or, from the all-in-one package (same code, one install):
import { min } from 'helpers4/array';
min(array: readonly number[] | null | undefined): number | undefined
ParameterTypeDescription
arrayreadonly number[] | null | undefinedArray of numbers

number | undefined — Minimum value, undefined for empty arrays, null, undefined, or NaN if any element is NaN

Unlike Math.min(…array), min() uses a loop and handles arrays of any size without stack overflow.

min([3, 1, 4, 1, 5, 9])
// => 1

// Safe for 1 000 000+ elements (Math.min(...arr) would throw):
min(Array.from({ length: 1_000_000 }, (_, i) => i))
// => 0

View source on GitHub