refactor(utils): enhance filterInPlace documentation

This commit is contained in:
productdevbook 2025-01-14 22:22:46 +03:00
parent d2f5e535ef
commit 3d36c99f61

View File

@ -4,14 +4,10 @@ export function toArray<T> (value: T | T[]): T[] {
} }
/** /**
* Turbo-charged in-place array filtering. This function mutates the array. * Filter out items from an array in place. This function mutates the array.
* Optimized for both small and large arrays using different strategies: * `predicate` get through the array from the end to the start for performance.
* - For small arrays (<=16 items): Uses splice for better memory efficiency
* - For large arrays: Uses optimized swap-and-truncate strategy
* *
* Processes array from end to start for maximum performance. * This function should be faster than `Array.prototype.filter` on large arrays.
* Benchmarks show this is significantly faster than Array.prototype.filter
* and standard filterInPlace implementations.
*/ */
export function filterInPlace<T> (array: T[], predicate: (item: T, index: number, arr: T[]) => unknown) { export function filterInPlace<T> (array: T[], predicate: (item: T, index: number, arr: T[]) => unknown) {
for (let i = array.length; i--; i >= 0) { for (let i = array.length; i--; i >= 0) {