Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: map filter -> reduce #438

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions src/hooks/useFilterTreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,21 @@ export default (
}

function dig(list: DefaultOptionType[], keepAll: boolean = false) {
return list
.map(dataNode => {
const children = dataNode[fieldChildren];

const match = keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode));
const childList = dig(children || [], match);

if (match || childList.length) {
return {
...dataNode,
isLeaf: undefined,
[fieldChildren]: childList,
};
}
return null;
})
.filter(node => node);
return list.reduce((total, dataNode) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

省一个 filter 是么?

Copy link
Contributor Author

@linxianxi linxianxi Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是滴,少遍历一次,这里是递归(就不止少遍历一次了)

const children = dataNode[fieldChildren];

const match = keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode));
const childList = dig(children || [], match);

if (match || childList.length) {
total.push({
...dataNode,
isLeaf: undefined,
[fieldChildren]: childList,
});
}
return total;
}, []);
}

return dig(treeData);
Expand Down
Loading