Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

perf(files): consolidate filtering and reduce temporary allocations (#8557)

brios723704e4fbbe
diff --git a/frontend/src/editor/core/components/filesPage/useLibraryFiles.ts b/frontend/src/editor/core/components/filesPage/useLibraryFiles.ts
index 2aa6b1a255..e2c9adf809 100644
--- a/frontend/src/editor/core/components/filesPage/useLibraryFiles.ts
+++ b/frontend/src/editor/core/components/filesPage/useLibraryFiles.ts
@@ -150,25 +150,38 @@ export function useLibraryFiles({
for (const f of diskEntries
? [...diskEntries, ...(stagedInScope ?? [])]
: filesInScope) {
- const ext = (f.name.split(".").pop() ?? "").toUpperCase();
+ const lastDot = f.name.lastIndexOf(".");
+ const ext = lastDot !== -1 ? f.name.slice(lastDot + 1).toUpperCase() : "";
if (ext) set.add(ext);
}
return Array.from(set).sort();
}, [filesInScope, diskEntries, stagedInScope, typeFilter]);
const visibleFiles = useMemo(() => {
- const filtered = filesInScope
- .filter((f) =>
- search ? f.name.toLowerCase().includes(search.toLowerCase()) : true,
- )
- .filter((f) =>
- originFilter === "all" ? true : getFileOrigin(f) === originFilter,
- )
- .filter((f) => {
- if (typeFilter.length === 0) return true;
- const ext = (f.name.split(".").pop() ?? "").toUpperCase();
- return typeFilter.includes(ext);
- });
+ const query = search ? search.toLowerCase() : null;
+ const hasTypeFilter = typeFilter.length > 0;
+ const typeSet = hasTypeFilter ? new Set(typeFilter) : null;
+ const hasOriginFilter = originFilter !== "all";
+
+ const filtered: typeof filesInScope = [];
+ for (let i = 0; i < filesInScope.length; i++) {
+ const f = filesInScope[i];
+ if (query && !f.name.toLowerCase().includes(query)) {
+ continue;
+ }
+ if (hasOriginFilter && getFileOrigin(f) !== originFilter) {
+ continue;
+ }
+ if (typeSet) {
+ const lastDot = f.name.lastIndexOf(".");
+ const ext =
+ lastDot !== -1 ? f.name.slice(lastDot + 1).toUpperCase() : "";
+ if (!typeSet.has(ext)) {
+ continue;
+ }
+ }
+ filtered.push(f);
+ }
return filtered.sort((a, b) => {
switch (sortMode) {
case "name-asc":
diff --git a/frontend/src/editor/core/services/zipFileService.ts b/frontend/src/editor/core/services/zipFileService.ts
index 24e06092f2..de84900dc0 100644
--- a/frontend/src/editor/core/services/zipFileService.ts
+++ b/frontend/src/editor/core/services/zipFileService.ts
@@ -478,9 +478,10 @@ export class ZipFileService {
const JSZip = await loadJSZip();
const zip = new JSZip();
const zipContents = await zip.loadAsync(zipBlob);
- const fileCount = Object.values(zipContents.files).filter(
- (entry) => !entry.dir,
- ).length;
+ let fileCount = 0;
+ for (const key in zipContents.files) {
+ if (!zipContents.files[key].dir) fileCount++;
+ }
return { shouldExtract: true, fileCount };
}
@@ -495,9 +496,10 @@ export class ZipFileService {
const zipContents = await zip.loadAsync(zipBlob);
// Count non-directory entries
- const fileCount = Object.values(zipContents.files).filter(
- (entry) => !entry.dir,
- ).length;
+ let fileCount = 0;
+ for (const key in zipContents.files) {
+ if (!zipContents.files[key].dir) fileCount++;
+ }
// Only extract if within limit
return {