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

[DataGrid] Filtering performance: cache values #9284

Merged
merged 7 commits into from
Jun 13, 2023
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { defaultMemoize } from 'reselect';
import {
GridCellParams,
GridFilterItem,
Expand Down Expand Up @@ -295,36 +296,36 @@ export const buildAggregatedFilterApplier = (
});
};

const filterModelItems = defaultMemoize(
romgrk marked this conversation as resolved.
Show resolved Hide resolved
(apiRef: React.MutableRefObject<GridApiCommunity>, items: GridFilterItem[]) =>
items.filter((item) => getFilterCallbackFromItem(item, apiRef) !== null),
);

const isValidFilterItemResult = (
romgrk marked this conversation as resolved.
Show resolved Hide resolved
result: null | GridFilterItemResult,
): result is GridFilterItemResult => result != null;
const isValidQuickFilterResult = (
result: null | GridQuickFilterValueResult,
): result is GridQuickFilterValueResult => result != null;

export const passFilterLogic = (
allFilterItemResults: (null | GridFilterItemResult)[],
allQuickFilterResults: (null | GridQuickFilterValueResult)[],
filterModel: GridFilterModel,
apiRef: React.MutableRefObject<GridApiCommunity>,
): boolean => {
const cleanedFilterItems = filterModel.items.filter(
(item) => getFilterCallbackFromItem(item, apiRef) !== null,
);

const cleanedAllFilterItemResults = allFilterItemResults.filter(
(result): result is GridFilterItemResult => result != null,
);

const cleanedAllQuickFilterResults = allQuickFilterResults.filter(
(result): result is GridQuickFilterValueResult => result != null,
);

// Defaultize operators
const quickFilterLogicOperator =
filterModel.quickFilterLogicOperator ?? getDefaultGridFilterModel().quickFilterLogicOperator;
const logicOperator = filterModel.logicOperator ?? getDefaultGridFilterModel().logicOperator;
const cleanedFilterItems = filterModelItems(apiRef, filterModel.items);
const cleanedFilterItemResults = allFilterItemResults.filter(isValidFilterItemResult);
const cleanedQuickFilterResults = allQuickFilterResults.filter(isValidQuickFilterResult);

// get result for filter items model
if (cleanedAllFilterItemResults.length > 0) {
if (cleanedFilterItemResults.length > 0) {
// Return true if the item pass with one of the rows
const filterItemPredicate = (item: GridFilterItem) => {
return cleanedAllFilterItemResults.some((filterItemResult) => filterItemResult[item.id!]);
return cleanedFilterItemResults.some((filterItemResult) => filterItemResult[item.id!]);
};

const logicOperator = filterModel.logicOperator ?? getDefaultGridFilterModel().logicOperator;
if (logicOperator === GridLogicOperator.And) {
const passesAllFilters = cleanedFilterItems.every(filterItemPredicate);
if (!passesAllFilters) {
Expand All @@ -339,14 +340,16 @@ export const passFilterLogic = (
}

// get result for quick filter model
if (cleanedAllQuickFilterResults.length > 0 && filterModel.quickFilterValues != null) {
if (cleanedQuickFilterResults.length > 0 && filterModel.quickFilterValues != null) {
// Return true if the item pass with one of the rows
const quickFilterValuePredicate = (value: string) => {
return cleanedAllQuickFilterResults.some(
return cleanedQuickFilterResults.some(
(quickFilterValueResult) => quickFilterValueResult[value],
);
};

const quickFilterLogicOperator =
filterModel.quickFilterLogicOperator ?? getDefaultGridFilterModel().quickFilterLogicOperator;
if (quickFilterLogicOperator === GridLogicOperator.And) {
const passesAllQuickFilterValues =
filterModel.quickFilterValues.every(quickFilterValuePredicate);
Expand Down