Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sortedIndexOf): Implement compat/sortedIndexOf #971

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Kyujenius
Copy link

@Kyujenius Kyujenius commented Mar 2, 2025

I added sortedIndexOf in compat layer using by sortedIndex which is

Resolves: #846

import { sortedIndex } from './sortedIndex';

/**
 * This method is like `indexOf` but performs a binary search on a sorted array.
 * @param {ArrayLike<T> | null | undefined} array The sorted array to inspect.
 * @param {T} value The value to search for.
 * @returns {number} Returns the index of the matched value, else -1.
 * @example
 *
 * sortedIndexOf([4,5,5,5,6], 5) => 1
 * sortedIndexOf([1.1, 2.2, 3.3], 2.2) => 1
 *
 */
export function sortedIndexOf<T>(array: ArrayLike<T> | null | undefined, value: T): number {
  if (!array?.length) return -1;

  const index = sortedIndex(array, value);
  if (index < array.length && areValuesEqual(array[index], value)) return index;
  return -1;
}

const areValuesEqual = <T>(a: T, b: T): boolean => Object.is(a, b);

I conducted more extensive testing, comparing all results with the existing Lodash functions. My tests covered a wider range of scenarios, and in all cases, my implementation produced identical results to the original Lodash functions.

Test

Test List Test Coverage
스크린샷 2025-03-03 오전 3 02 29 스크린샷 2025-03-03 오전 3 02 57
Bench Test (It is 1.7x~ 1.8x faster than lodash )
benchTest

@Kyujenius Kyujenius requested a review from raon0211 as a code owner March 2, 2025 18:22
Copy link

vercel bot commented Mar 2, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
es-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 12, 2025 1:48pm

@Kyujenius Kyujenius changed the title feat(compat): Implement sortedIndexOf feat(sortedIndexOf): Implement compat/sortedIndexOf Mar 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support sortedIndex, sortedIndexBy, sortedIndexOf
1 participant