Skip to content

Commit e083794

Browse files
committed
chore: Fix some O(n^2) behavior
1 parent bf8b079 commit e083794

4 files changed

Lines changed: 26 additions & 23 deletions

File tree

mobile/src/data/album/queries.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ export function useAlbumForScreen(albumId: string) {
1818
return useQuery({
1919
...q.albums.detail(albumId),
2020
select: ({ name, artistsKey, artwork, isFavorite, tracks, year }) => {
21-
const albumArtists = AlbumArtistsKey.deconstruct(artistsKey);
22-
21+
const albumArtists = new Set(AlbumArtistsKey.deconstruct(artistsKey));
2322
return {
2423
name,
2524
imageSource: artwork,
@@ -34,15 +33,15 @@ export function useAlbumForScreen(albumId: string) {
3433
tracks: tracks.map(({ name: title, duration, artists, ...other }) => {
3534
let description = formatSeconds(duration);
3635
if (Array.isArray(artists)) {
37-
const diff = artists.filter((name) => !albumArtists.includes(name));
36+
const diff = artists.filter((name) => !albumArtists.has(name));
3837
if (diff.length > 0) description += ` • ${diff.join(", ")}`;
3938
}
4039

4140
const { artwork: _, albumName: _1, ...rest } = other;
4241
return { ...rest, title, description, imageSource: null };
4342
}),
4443

45-
artists: albumArtists,
44+
artists: [...albumArtists],
4645
isFavorite,
4746
};
4847
},

mobile/src/navigation/screens/playlists/components/ModifyViewBase.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export function ModifyPlaylistBase(props: {
5454
// Exclude `FavoritesPlaylistKey` as we don't return it when fetching all
5555
// playlists & don't check it in `sanitizePlaylistName`.
5656
const usedNames = useMemo(
57-
() => [...props.referenceData.playlistNames!, FavoritesPlaylistKey],
57+
() =>
58+
new Set([...props.referenceData.playlistNames!, FavoritesPlaylistKey]),
5859
[props.referenceData.playlistNames],
5960
);
6061

@@ -80,7 +81,7 @@ export function ModifyPlaylistBase(props: {
8081
const sanitized = sanitizePlaylistName(name);
8182
isUnique =
8283
props.initialData?.name === sanitized ||
83-
!usedNames.includes(sanitized);
84+
!usedNames.has(sanitized);
8485
} catch {}
8586
return isUnique;
8687
}}

mobile/src/navigation/screens/tracks/sheets/TrackToPlaylistsSheet.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useMemo } from "react";
2+
13
import { usePlaylistsNames } from "~/data/playlist/queries";
24
import {
35
useToggleTrackInPlaylist,
@@ -22,6 +24,8 @@ export function TrackToPlaylistsSheet({ id }: { id: string }) {
2224
const toggleInPlaylist = useToggleTrackInPlaylist(id);
2325
const sheetListHandlers = useEnableSheetScroll();
2426

27+
const inListSet = useMemo(() => new Set(inList ?? []), [inList]);
28+
2529
return (
2630
<DetachedSheet
2731
globalKey={GLOBAL_SHEET_KEY}
@@ -31,21 +35,18 @@ export function TrackToPlaylistsSheet({ id }: { id: string }) {
3135
<FlatList
3236
data={playlistsNames}
3337
keyExtractor={(name) => name}
34-
extraData={inList}
35-
renderItem={({ item: name }) => {
36-
const selected = inList?.includes(name) ?? false;
37-
return (
38-
<CheckboxField
39-
checked={selected}
40-
onCheck={() => mutateGuard(toggleInPlaylist, name)}
41-
className="mb-2"
42-
>
43-
<Marquee color="surfaceBright">
44-
<StyledText>{name}</StyledText>
45-
</Marquee>
46-
</CheckboxField>
47-
);
48-
}}
38+
extraData={inListSet}
39+
renderItem={({ item: name }) => (
40+
<CheckboxField
41+
checked={inListSet.has(name)}
42+
onCheck={() => mutateGuard(toggleInPlaylist, name)}
43+
className="mb-2"
44+
>
45+
<Marquee color="surfaceBright">
46+
<StyledText>{name}</StyledText>
47+
</Marquee>
48+
</CheckboxField>
49+
)}
4950
getItemLayout={getItemLayout}
5051
ListEmptyComponent={
5152
<ContentPlaceholder errMsgKey="err.msg.noPlaylists" />

mobile/src/utils/object.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ export function pickKeys<T extends Record<PropertyKey, any>, K extends keyof T>(
4949
obj: T,
5050
keys: readonly K[],
5151
) {
52+
const keySet = new Set(keys);
5253
return Object.fromEntries(
53-
Object.entries(obj).filter(([key, _val]) => keys.includes(key as K)),
54+
Object.entries(obj).filter(([key, _val]) => keySet.has(key as K)),
5455
) as Pick<T, K>;
5556
}
5657

@@ -59,7 +60,8 @@ export function omitKeys<T extends Record<PropertyKey, any>, K extends keyof T>(
5960
obj: T,
6061
keys: readonly K[],
6162
) {
63+
const keySet = new Set(keys);
6264
return Object.fromEntries(
63-
Object.entries(obj).filter(([key, _val]) => !keys.includes(key as K)),
65+
Object.entries(obj).filter(([key, _val]) => !keySet.has(key as K)),
6466
) as Omit<T, K>;
6567
}

0 commit comments

Comments
 (0)