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

Support pullAllWith #977

Open
leehj322 opened this issue Mar 8, 2025 · 1 comment
Open

Support pullAllWith #977

leehj322 opened this issue Mar 8, 2025 · 1 comment

Comments

@leehj322
Copy link

leehj322 commented Mar 8, 2025

Hi, @raon0211

I would like to implement the pullAllWith method for issue #91.

Would it be okay to implement it in the compat layer?

Suggestion

Option1 (Modify the array in-place using resultIndex)

export function pullAllWith<T>(array: T[], values: T[], comparator: (a: T, b: T) => boolean): T[] {
  let resultIndex = 0;

  for (let i = 0; i < array.length; i++) {
    let shouldRemove = false;
    for (let j = 0; j < values.length; j++) {
      if (comparator(array[i], values[j])) {
        shouldRemove = true;
        break;
      }
    }
    if (!shouldRemove) {
      array[resultIndex++] = array[i];
    }
  }

  array.length = resultIndex; // Resize the array

  return array;
}

Option2 (Create a new filtered array)

export function pullAllWith<T>(array: T[], values: T[], comparator: (a: T, b: T) => boolean): T[] {
  const modifiedArray = array.filter(item => !values.some(value => comparator(item, value)));
  array.length = 0; // Reset array
  array.push(...modifiedArray);

  return array;
}

I expect Option1 to be more performant but slightly harder to read.
I'd love to hear your thoughts on which option would be more appropriate!

@raon0211
Copy link
Collaborator

raon0211 commented Mar 9, 2025

What about we check each function's performance using benchmarks (you can see other benchmarks in our benchmark folder) and try with the more performant one? I think the first one is fine, it is not too complicated.

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

No branches or pull requests

2 participants