2

The MUI documentation has a table of benchmark cases here. But the different cases aren't clear to me. Please explain what the differences are with real examples between the following:

  1. <Div>
  2. <StyledDiv>
  3. <Box sx={…}>

I am assuming these correlate to:

  1. <Box>
  2. <Box mb={2}>
  3. <Box sx={{mb:2}>

If this is correct and you are focusing on performance, then you should favor system properties over the sx property. However, only a few MUI components support system properties while all support sx. This has led my team to using Box in combination with the component property thinking it is better for performance, 181ms instead of 296 ms. For example,

  • Instead of <Paper sx={{mb:2}}>
  • We use <Box component={Paper} mb={2}>

Inspecting the components using React Dev Tools, it seems like the system properties are converted directly into the sx property meaning my team's decision is not better for performance and does not correlate to <StyledDiv>.

Box with system property

The Box with mb={2} passed as a property in turn renders a div styled that has mb:2 inside the sx property.

Styled div with sx property

It is also worth noting, that the <Box component={Paper} mb={2}> approach rather than <Paper sx={{mb:2}}> while it doesn't create additional DOM elements there are additional components.

Box

versus

Paper

So should you favor system properties over the sx property or not?

Bro3Simon
  • 129
  • 10
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 24 '23 at 14:19

1 Answers1

2

Below are examples of what the documentation means when referring to Div, StyledDiv, and Box.

import { styled } from "@mui/material/styles";
import Box from "@mui/material/Box";

function Div() {
  return <div>I'm a simple Div component</div>;
}
const StyledDiv = styled("div")`
  border: black solid 1px;
`;
export default function App() {
  return (
    <>
      <Div />
      <StyledDiv>I'm a StyledDiv</StyledDiv>
      <Box sx={{ border: "purple solid 1px" }}>I'm a Box</Box>
    </>
  );
}

Edit benchmark component examples

The differences in the benchmarks have nothing to do with using sx vs. system properties -- those are basically just two syntax alternatives for the same underlying functionality and I wouldn't expect any significant performance difference between the two. I would expect that wrapping other Material-UI components with Box in order to use system properties instead of sx would be worse performance than using sx on the initial Material-UI component (e.g. Paper).

Related answer: Why is the `sx` prop so much slower?

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198