Skip to content

Commit

Permalink
pills: Optimize removePill function.
Browse files Browse the repository at this point in the history
The removePill function of input_pill module uses a for loop
for iterating and finding the required pill.
Replace the for loop with inbuilt findIndex function for efficiency.

Authored-by: Dhruv Chouhan <[email protected]>.
  • Loading branch information
ecxtacy authored and timabbott committed May 17, 2024
1 parent ac7dd6f commit 6af748f
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions web/src/input_pill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,9 @@ export function create<T>(opts: InputPillCreateOptions<T>): InputPillContainer<T
// this would generally be used for DOM-provoked actions, such as a user
// clicking on a pill to remove it.
removePill(element: HTMLElement) {
let idx: number | undefined;
for (let x = 0; x < store.pills.length; x += 1) {
if (store.pills[x].$element[0] === element) {
idx = x;
}
}
const idx = store.pills.findIndex((pill) => pill.$element[0] === element);

if (idx !== undefined) {
if (idx !== -1) {
store.pills[idx].$element.remove();
const pill = store.pills.splice(idx, 1);
if (store.onPillRemove !== undefined) {
Expand Down

0 comments on commit 6af748f

Please sign in to comment.