Skip to content

Commit

Permalink
perf(ui): useMemo for filtering & sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Apr 11, 2020
1 parent 36b2f7b commit 9495337
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
10 changes: 6 additions & 4 deletions packages/ui/src/components/bundle-assets/bundle-assets.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { get, map } from 'lodash';
Expand Down Expand Up @@ -149,9 +149,11 @@ export const BundleAssets = (props) => {
updateSearch('');
};

const emptyMessage = (
<EmptySet resources="assets" filtered={totalRowCount !== 0} resetFilters={clearSearch} />
const emptyMessage = useMemo(
() => <EmptySet resources="assets" filtered={totalRowCount !== 0} resetFilters={clearSearch} />,
[],
);
const renderRowHeader = useMemo(() => getRenderRowHeader(map(jobs, 'label')), []);

return (
<section className={cx(css.root, className)}>
Expand Down Expand Up @@ -214,7 +216,7 @@ export const BundleAssets = (props) => {
<MetricsTable
runs={jobs}
items={items}
renderRowHeader={getRenderRowHeader(map(jobs, 'label'))}
renderRowHeader={renderRowHeader}
emptyMessage={emptyMessage}
showHeaderSum
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import { map } from 'lodash';
Expand Down Expand Up @@ -59,8 +59,12 @@ export const BundleChunkModules = ({
updateSearch('');
};

const emptyMessage = (
<EmptySet resources="modules" filtered={totalRowCount !== 0} resetFilters={clearSearch} />
const renderRowHeader = useMemo(() => getRenderRowHeader(map(runs, 'label')), []);
const emptyMessage = useMemo(
() => (
<EmptySet resources="modules" filtered={totalRowCount !== 0} resetFilters={clearSearch} />
),
[],
);

return (
Expand Down Expand Up @@ -112,7 +116,7 @@ export const BundleChunkModules = ({
className={css.table}
items={items}
runs={runs}
renderRowHeader={getRenderRowHeader(map(runs, 'label'))}
renderRowHeader={renderRowHeader.current}
emptyMessage={emptyMessage}
showHeaderSum
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';

Expand Down Expand Up @@ -32,8 +32,9 @@ export const BundlePackages = (props) => {
updateSearch('');
};

const emptyMessage = (
<EmptySet resources="packages" filtered={totalRowCount !== 0} resetFilters={clear} />
const emptyMessage = useMemo(
() => <EmptySet resources="packages" filtered={totalRowCount !== 0} resetFilters={clear} />,
[],
);

return (
Expand Down
8 changes: 6 additions & 2 deletions packages/ui/src/hocs/with-custom-sort.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import { orderBy } from 'lodash';

Expand All @@ -11,7 +11,11 @@ export const withCustomSort = ({
const WithCustomSort = (props) => {
const { items } = props;
const [sort, updateSort] = useState({ sortBy, direction });
const orderedItems = orderBy(items, getCustomSort(sort.sortBy), sort.direction);
const orderedItems = useMemo(() => orderBy(items, getCustomSort(sort.sortBy), sort.direction), [
items,
sort.sortBy,
sort.direction,
]);

const otherProps = {
sort,
Expand Down
18 changes: 11 additions & 7 deletions packages/ui/src/hocs/with-filtered-items.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import React from 'react';
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';

export const withFilteredItems = (getFilter) => (BaseComponent) => {
const WithFilteredItems = (props) => {
const { items, filters, searchPattern } = props;

const filteredItems = items.filter((item) => {
if (searchPattern && !searchPattern.test(item?.key)) {
return false;
}
const filteredItems = useMemo(
() =>
items.filter((item) => {
if (searchPattern && !searchPattern.test(item?.key)) {
return false;
}

return getFilter(filters)(item);
});
return getFilter(filters)(item);
}),
[items, searchPattern, filters],
);

const baseProps = {
items: filteredItems,
Expand Down

0 comments on commit 9495337

Please sign in to comment.