Toolbox-XToolbox-X

Find One Item

Finds the first item matching specified criteria using optimized search strategies.

findOne()

Finds the first item matching specified criteria using optimized search strategies.

Signature

findOne(
  matcher: string | number,
  keySelector: KeySelector<T>,
  options?: FindOptions<T>
): T | undefined

Parameters

ParameterTypeDescription
matcherstring | numberValue to match against.
keySelectorKeySelector<T>Property name or value extractor function.
optionsFindOptions<T>Search configuration options.

Returns

T | undefined — First matching item or undefined if none found.

Example Usage

playground.ts

Behavior Details

  1. Cache First: Checks cache if cacheKey is provided.
  2. Search Strategies:
    • Linear search for small datasets (less than 100 items).
    • Binary search for larger sorted datasets.
    • Fuzzy search when enabled and no exact match.
  3. Result Caching: Stores successful finds when cacheKey is specified.

Notes

  • Performance: Uses cache when available ($O(1)$ lookup). Falls back to linear search for small datasets ($O(n)$) or binary search for large sorted datasets ($O(\log n)$). Fuzzy search is $O(n)$ when enabled.
  • Cache Behavior: Cache entries expire after 5 minutes by default. Cache is automatically cleared when the dataset changes.
  • String Matching: Case-insensitive by default. Exact matching unless fuzzy is true.
  • Sorting: Sorts data automatically when needSorting is true. Skips sorting when data is already sorted.

Last updated: Mon, Jun 15, 2026 06:09:51AM (UTC)

On this page