perf: replace Array#map Array#filter chain w/ Array#reduce (#1203)

This commit is contained in:
Sukka 2024-06-15 12:22:33 +08:00 committed by GitHub
parent 481e473b60
commit 4f5227782a
3 changed files with 58 additions and 36 deletions

View File

@ -24,11 +24,11 @@ const LogPage = () => {
const [match, setMatch] = useState(() => (_: string) => true); const [match, setMatch] = useState(() => (_: string) => true);
const filterLogs = useMemo(() => { const filterLogs = useMemo(() => {
return logData return logData.filter(
.filter((data) => (data) =>
logState === "all" ? true : data.type.includes(logState) (logState === "all" ? true : data.type.includes(logState)) &&
) match(data.payload)
.filter((data) => match(data.payload)); );
}, [logData, logState, match]); }, [logData, logState, match]);
return ( return (

View File

@ -134,25 +134,48 @@ export const getProxies = async () => {
const { GLOBAL: global, DIRECT: direct, REJECT: reject } = proxyRecord; const { GLOBAL: global, DIRECT: direct, REJECT: reject } = proxyRecord;
let groups = Object.values(proxyRecord) interface Group {
.filter((each) => each.name !== "GLOBAL" && each.all) all: IProxyItem[];
.map((each) => ({ name: string;
...each, type: string;
all: each.all!.map((item) => generateItem(item)), udp: boolean;
})); xudp: boolean;
tfo: boolean;
history: {
time: string;
delay: number;
}[];
}
let groups: Group[] = Object.values(proxyRecord).reduce<Group[]>(
(acc, each) => {
if (each.name !== "GLOBAL" && each.all) {
acc.push({
...each,
all: each.all!.map((item) => generateItem(item)),
});
}
return acc;
},
[]
);
if (global?.all) { if (global?.all) {
let globalGroups = global.all let globalGroups: Group[] = global.all.reduce<Group[]>((acc, name) => {
.filter((name) => proxyRecord[name]?.all) if (proxyRecord[name]?.all) {
.map((name) => proxyRecord[name]) acc.push({
.map((each) => ({ ...proxyRecord[name],
...each, all: proxyRecord[name].all!.map((item) => generateItem(item)),
all: each.all!.map((item) => generateItem(item)), });
})); }
let globalNames = globalGroups.map((each) => each.name); return acc;
}, []);
let globalNames = new Set(globalGroups.map((each) => each.name));
groups = groups groups = groups
.filter((group) => { .filter((group) => {
return !globalNames.includes(group.name); return !globalNames.has(group.name);
}) })
.concat(globalGroups); .concat(globalGroups);
} }

View File

@ -7,23 +7,22 @@ export async function getClashLogs() {
const newRegex = /(.+?)\s+(.+?)\s+(.+)/; const newRegex = /(.+?)\s+(.+?)\s+(.+)/;
const logs = await invoke<string[]>("get_clash_logs"); const logs = await invoke<string[]>("get_clash_logs");
return logs return logs.reduce<ILogItem[]>((acc, log) => {
.map((log) => { const result = log.match(regex);
const result = log.match(regex); if (result) {
if (result) { const [_, _time, type, payload] = result;
const [_, _time, type, payload] = result; const time = dayjs(_time).format("MM-DD HH:mm:ss");
const time = dayjs(_time).format("MM-DD HH:mm:ss"); acc.push({ time, type, payload });
return { time, type, payload }; return acc;
} }
const result2 = log.match(newRegex); const result2 = log.match(newRegex);
if (result2) { if (result2) {
const [_, time, type, payload] = result2; const [_, time, type, payload] = result2;
return { time, type, payload }; acc.push({ time, type, payload });
} }
return null; return acc;
}) }, []);
.filter(Boolean) as ILogItem[];
} }
export async function getProfiles() { export async function getProfiles() {