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

[core] Only clone props if needed #24892

Merged
merged 3 commits into from Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions benchmark/browser/scenarios/table-cell/index.js
@@ -0,0 +1,38 @@
import * as React from 'react';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';

const data = { name: 'Frozen yoghurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 };
const rows = Array.from(new Array(100)).map(() => data);

export default function TableCellCase() {
return (
<Table>
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell>Calories</TableCell>
<TableCell>Fat&nbsp;(g)</TableCell>
<TableCell>Carbs&nbsp;(g)</TableCell>
<TableCell>Protein&nbsp;(g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map((row, index) => (
<TableRow key={index}>
<TableCell component="th" scope="row">
{row.name}
</TableCell>
<TableCell>{row.calories}</TableCell>
<TableCell>{row.fat}</TableCell>
<TableCell>{row.carbs}</TableCell>
<TableCell>{row.protein}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}
4 changes: 4 additions & 0 deletions benchmark/browser/scripts/benchmark.js
Expand Up @@ -154,6 +154,10 @@ async function run() {
name: 'noop (baseline)',
path: './noop/index.js',
},
{
name: 'Table',
path: './table-cell/index.js',
},
// Test the cost of React primitives
{
name: 'React primitives',
Expand Down
4 changes: 1 addition & 3 deletions docs/pages/performance/table-component.js
Expand Up @@ -25,7 +25,7 @@ const TableBody = createComponent('tbody');
const data = { name: 'Frozen yoghurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 };
const rows = Array.from(new Array(100)).map(() => data);

function TableComponent() {
export default function TableComponent() {
return (
<NoSsr defer>
<Table>
Expand Down Expand Up @@ -55,5 +55,3 @@ function TableComponent() {
</NoSsr>
);
}

export default TableComponent;
4 changes: 1 addition & 3 deletions docs/pages/performance/table-emotion.js
Expand Up @@ -31,7 +31,7 @@ const TableBody = createComponent('tbody');
const data = { name: 'Frozen yoghurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 };
const rows = Array.from(new Array(100)).map(() => data);

function TableEmotion() {
export default function TableEmotion() {
return (
<NoSsr defer>
<Table>
Expand Down Expand Up @@ -61,5 +61,3 @@ function TableEmotion() {
</NoSsr>
);
}

export default TableEmotion;
4 changes: 1 addition & 3 deletions docs/pages/performance/table-hook.js
Expand Up @@ -33,7 +33,7 @@ const TableBody = createComponent('tbody');
const data = { name: 'Frozen yoghurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 };
const rows = Array.from(new Array(100)).map(() => data);

function TableHook() {
export default function TableHook() {
return (
<NoSsr defer>
<Table>
Expand Down Expand Up @@ -63,5 +63,3 @@ function TableHook() {
</NoSsr>
);
}

export default TableHook;
4 changes: 1 addition & 3 deletions docs/pages/performance/table-mui.js
Expand Up @@ -9,7 +9,7 @@ import TableRow from '@material-ui/core/TableRow';
const data = { name: 'Frozen yoghurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 };
const rows = Array.from(new Array(100)).map(() => data);

function TableMui() {
export default function TableMui() {
return (
<NoSsr defer>
<Table>
Expand Down Expand Up @@ -39,5 +39,3 @@ function TableMui() {
</NoSsr>
);
}

export default TableMui;
4 changes: 1 addition & 3 deletions docs/pages/performance/table-raw.js
Expand Up @@ -4,7 +4,7 @@ import NoSsr from '@material-ui/core/NoSsr';
const data = { name: 'Frozen yoghurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 };
const rows = Array.from(new Array(100)).map(() => data);

function TableRaw() {
export default function TableRaw() {
return (
<NoSsr defer>
<table>
Expand Down Expand Up @@ -32,5 +32,3 @@ function TableRaw() {
</NoSsr>
);
}

export default TableRaw;
4 changes: 1 addition & 3 deletions docs/pages/performance/table-styled-components.js
Expand Up @@ -28,7 +28,7 @@ const TableBody = createComponent('tbody');
const data = { name: 'Frozen yoghurt', calories: 159, fat: 6.0, carbs: 24, protein: 4.0 };
const rows = Array.from(new Array(100)).map(() => data);

function TableStyledComponents() {
export default function TableStyledComponents() {
return (
<NoSsr defer>
<Table>
Expand Down Expand Up @@ -58,5 +58,3 @@ function TableStyledComponents() {
</NoSsr>
);
}

export default TableStyledComponents;
Expand Up @@ -11,16 +11,18 @@ export default function getThemeProps(params) {
return props;
}

const output = { ...props };

// Resolve default props, code borrow from React source.
// https://github.com/facebook/react/blob/15a8f031838a553e41c0b66eb1bcf1da8448104d/packages/react/src/ReactElement.js#L221
const defaultProps = theme.components[name].defaultProps;
let propName;

for (propName in defaultProps) {
if (props[propName] === undefined) {
props[propName] = defaultProps[propName];
if (output[propName] === undefined) {
output[propName] = defaultProps[propName];
}
}

return props;
return output;
}
2 changes: 1 addition & 1 deletion packages/material-ui/src/Modal/Modal.js
Expand Up @@ -55,7 +55,7 @@ export const styles = (theme) => ({
*/
const Modal = React.forwardRef(function Modal(inProps, ref) {
const theme = useTheme();
const props = getThemeProps({ name: 'MuiModal', props: { ...inProps }, theme });
const props = getThemeProps({ name: 'MuiModal', props: inProps, theme });
const {
BackdropComponent = SimpleBackdrop,
BackdropProps,
Expand Down
Expand Up @@ -135,7 +135,7 @@ const transitionDurationDefault = { enter: duration.enteringScreen, exit: durati

const SwipeableDrawer = React.forwardRef(function SwipeableDrawer(inProps, ref) {
const theme = useTheme();
const props = getThemeProps({ name: 'MuiSwipeableDrawer', props: { ...inProps }, theme });
const props = getThemeProps({ name: 'MuiSwipeableDrawer', props: inProps, theme });
const {
anchor = 'left',
disableBackdropTransition = false,
Expand Down
9 changes: 2 additions & 7 deletions packages/material-ui/src/styles/useThemeProps.js
Expand Up @@ -2,19 +2,14 @@ import { getThemeProps } from '@material-ui/styles';
import useTheme from './useTheme';
import defaultTheme from './defaultTheme';

export default function useThemeProps({ props: inputProps, name }) {
const props = { ...inputProps };

export default function useThemeProps({ props, name }) {
const contextTheme = useTheme() || defaultTheme;

const more = getThemeProps({ theme: contextTheme, name, props });

const theme = more.theme || contextTheme;
const isRtl = theme.direction === 'rtl';

return {
theme,
isRtl,
isRtl: theme.direction === 'rtl',
...more,
};
}
2 changes: 1 addition & 1 deletion packages/material-ui/src/withWidth/withWidth.js
Expand Up @@ -37,7 +37,7 @@ const withWidth = (options = {}) => (Component) => {
const { initialWidth, width, ...other } = getThemeProps({
theme,
name: 'MuiWithWidth',
props: { ...props },
props,
});

const [mountedState, setMountedState] = React.useState(false);
Expand Down
@@ -1,5 +1,5 @@
export default function Modal(inProps) {
const props = getThemeProps({ props: { ...inProps } });
const props = getThemeProps({ props: inProps });
const { onKeyDown, ...other } = props;

function handleKeyDown(event) {
Expand Down
@@ -1,6 +1,6 @@
import PropTypes from 'prop-types';
function Modal(inProps) {
const props = getThemeProps({ props: { ...inProps } });
const props = getThemeProps({ props: inProps });
const { onKeyDown, ...other } = props;

function handleKeyDown(event) {
Expand Down