Use efficient filters
This commit is contained in:
parent
07cb8491e2
commit
4005da4849
13 changed files with 82 additions and 47 deletions
17
frontend/sync-client/src/utils/remove-from-array.ts
Normal file
17
frontend/sync-client/src/utils/remove-from-array.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/**
|
||||
* Efficiently removes a specific item from an array by modifying it in place.
|
||||
* This is more efficient than using `.filter(item => item !== toRemove)` as it avoids creating a new array
|
||||
*
|
||||
* @param array The array to modify
|
||||
* @param item The item to remove
|
||||
* @returns true if the item was found and removed, false otherwise
|
||||
*/
|
||||
export function removeFromArray<T>(array: T[], item: T): boolean {
|
||||
const index = array.indexOf(item);
|
||||
if (index !== -1) {
|
||||
// eslint-disable-next-line no-restricted-syntax -- This is the implementation of the helper itself
|
||||
array.splice(index, 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue