Skip to content

Commit

Permalink
perf: reduce iterations and allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
ferferga authored Oct 26, 2024
1 parent c992cbc commit 1ac7877
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion frontend/src/components/Playback/TrackList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<tbody>
<template v-for="(tracksOnDisc, discNumber) in tracksPerDisc">
<tr
v-if="Object.keys(tracksPerDisc).length > 1"
v-if="hasMultipleDiscs"
:key="discNumber"
class="disc-header">
<td
Expand Down Expand Up @@ -125,6 +125,7 @@ import { useBaseItem } from '@/composables/apis';
import { playbackManager } from '@/store/playback-manager';
import { getItemDetailsLink } from '@/utils/items';
import { formatTicks } from '@/utils/time';
import { isEmpty } from '@/utils/validation';
const { item } = defineProps<{
item: BaseItemDto;
Expand All @@ -137,6 +138,7 @@ const { data: tracks } = await useBaseItem(getItemsApi, 'getItems')(() => ({
}));
const tracksPerDisc = computed(() => Object.groupBy(tracks.value, ({ ParentIndexNumber }) => ParentIndexNumber!));
const hasMultipleDiscs = computed(() => !isEmpty(tracksPerDisc.value));
/**
* Check if a given BaseItemDto is playing
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/plugins/workers/generic/subtitles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ function replaceTags(input: string, tagMap: TagMap) {
let formattedText = input;

// Iterate through tag mappings
for (const [htmlTag, markdownTag] of Object.entries(tagMap)) {
for (const htmlTag in tagMap) {
const escapedHtmlTag = htmlTag.replaceAll('\\', '\\\\');
const regex = new RegExp(escapedHtmlTag, 'gi');

formattedText = formattedText.replace(regex, (_, p1: string) => {
return markdownTag.replace('$1', p1.trim());
return tagMap[htmlTag].replace('$1', p1.trim());
});
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/store/playback-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,12 +1133,12 @@ class PlaybackManagerStore extends CommonStore<PlaybackManagerState> {
}
};

for (const [action, handler] of Object.entries(actionHandlers)) {
for (const action in actionHandlers) {
try {
window.navigator.mediaSession.setActionHandler(
action as MediaSessionAction,
/* eslint-disable-next-line unicorn/no-null */
add ? handler : null
add ? actionHandlers[action as keyof typeof actionHandlers] ?? null : null
);
} catch {
console.error(
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ export function isObj(value: unknown): value is object {
return typeof value === 'object' && !isNull(value);
}

/**
* Check if an object is empty
*/
export function isEmpty(value: object): boolean {
for (const _ in value) {
return false;
}

return true;
}

/**
* TypeScript type guard for AxiosError
*/
Expand Down

0 comments on commit 1ac7877

Please sign in to comment.