Skip to content

Commit

Permalink
perf: reduce iterations and allocations (#2481)
Browse files Browse the repository at this point in the history
* Object.keys and Object.values were used in some for loops,
which increased allocations and memory pressure unnecessarily.
  • Loading branch information
ferferga authored Oct 26, 2024
1 parent c992cbc commit 410358c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
15 changes: 14 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 @@ -137,6 +137,19 @@ const { data: tracks } = await useBaseItem(getItemsApi, 'getItems')(() => ({
}));
const tracksPerDisc = computed(() => Object.groupBy(tracks.value, ({ ParentIndexNumber }) => ParentIndexNumber!));
const hasMultipleDiscs = computed(() => {
let loops = 0;
for (const _ in tracksPerDisc.value) {
loops++;
if (loops > 1) {
return true;
}
}
return false;
});
/**
* 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
13 changes: 12 additions & 1 deletion packages/configs/eslint/rules/typescript-vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import eslintImportX from 'eslint-plugin-import-x';
// @ts-expect-error - No types available
import vueScopedCSS from 'eslint-plugin-vue-scoped-css';
import css from 'eslint-plugin-css';
// @ts-expect-error - No types available
import vue from 'eslint-plugin-vue';
// @ts-expect-error - No types available
import promise from 'eslint-plugin-promise';
Expand Down Expand Up @@ -94,6 +93,18 @@ const common = [
}],
'@typescript-eslint/no-confusing-void-expression': ['error', { ignoreArrowShorthand: true }],
'@typescript-eslint/no-empty-object-type': ['error', { allowInterfaces: 'with-single-extends' }],
'@typescript-eslint/no-unused-vars': [
'error',
{
args: 'all',
argsIgnorePattern: '^_',
caughtErrors: 'all',
caughtErrorsIgnorePattern: '^_',
destructuredArrayIgnorePattern: '^_',
varsIgnorePattern: '^_',
ignoreRestSiblings: true
}
],
'vue/return-in-computed-property': 'off'
}
},
Expand Down

0 comments on commit 410358c

Please sign in to comment.