Skip to content

Commit a4a4476

Browse files
authored
[system] Add browser benchmark (#22923)
1 parent 0fb80b8 commit a4a4476

File tree

22 files changed

+612
-5
lines changed

22 files changed

+612
-5
lines changed

benchmark/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Browser benchmark
2+
3+
This project is used when running the following command:
4+
5+
```sh
6+
yarn benchmark
7+
```
8+
9+
It is suppose to give developers comparable values between running different scenarios inside the browser, that can be find the `./scenarios` folder.
10+
11+
You should use these numbers exclusively for comparing performance between different scenarios, not as absolute values. There is also a `./noop` scenario, that renders nothing, to give you the idea of the initial setup time before the actual code is being run.

benchmark/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Perf scenario</title>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="initial-scale=1, width=device-width" />
7+
<style>body { background-color: white; }</style>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script src="../../tmp/benchmark.js"></script>
12+
</body>
13+
</html>

benchmark/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import PropTypes from 'prop-types';
4+
5+
// Get all the scenarios
6+
const requirePerfScenarios = require.context('./scenarios', true, /(js|ts|tsx)$/);
7+
8+
const rootEl = document.getElementById('root');
9+
10+
const scenarioSuitePath = window.location.search.replace('?', '');
11+
12+
const Component = requirePerfScenarios(scenarioSuitePath).default;
13+
14+
const start = performance.now();
15+
let end;
16+
17+
function TestCase(props) {
18+
const ref = React.useRef(null);
19+
20+
React.useLayoutEffect(() => {
21+
// Force layout
22+
ref.current.getBoundingClientRect();
23+
24+
end = performance.now();
25+
window.timing = {
26+
render: end - start,
27+
};
28+
});
29+
30+
return <div ref={ref}>{props.children}</div>;
31+
}
32+
33+
TestCase.propTypes = {
34+
children: PropTypes.node,
35+
};
36+
37+
ReactDOM.render(
38+
<TestCase>
39+
<Component />
40+
</TestCase>,
41+
rootEl,
42+
);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as React from 'react';
2+
import { createMuiTheme } from '@material-ui/core/styles';
3+
import styledEmotion from '@emotion/styled';
4+
import { ThemeProvider as EmotionTheme } from 'emotion-theming';
5+
import { styleFunction } from '@material-ui/core/Box';
6+
import { logReactMetrics } from '../utils';
7+
8+
const materialSystemTheme = createMuiTheme();
9+
const Box = styledEmotion('div')(styleFunction);
10+
11+
export default function BoxEmotion() {
12+
return (
13+
<React.Profiler id="box-emotion" onRender={logReactMetrics}>
14+
{new Array(1000).fill().map(() => (
15+
<EmotionTheme theme={materialSystemTheme}>
16+
<Box
17+
color="primary.main"
18+
bgcolor="background.paper"
19+
fontFamily="h6.fontFamily"
20+
fontSize={['h6.fontSize', 'h4.fontSize', 'h3.fontSize']}
21+
p={[2, 3, 4]}
22+
>
23+
emotion
24+
</Box>
25+
</EmotionTheme>
26+
))}
27+
</React.Profiler>
28+
);
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as React from 'react';
2+
import { createMuiTheme } from '@material-ui/core/styles';
3+
import { ThemeProvider as StylesThemeProvider } from '@material-ui/styles';
4+
import BoxStyles from '@material-ui/core/Box';
5+
import { logReactMetrics } from '../utils';
6+
7+
const materialSystemTheme = createMuiTheme();
8+
9+
export default function BoxMaterialUIStyles() {
10+
return (
11+
<React.Profiler id="box-material-ui-system" onRender={logReactMetrics}>
12+
{new Array(1000).fill().map(() => (
13+
<StylesThemeProvider theme={materialSystemTheme}>
14+
<BoxStyles
15+
color="primary.main"
16+
bgcolor="background.paper"
17+
fontFamily="h6.fontFamily"
18+
fontSize={['h6.fontSize', 'h4.fontSize', 'h3.fontSize']}
19+
p={[2, 3, 4]}
20+
>
21+
@material-ui/styles
22+
</BoxStyles>
23+
</StylesThemeProvider>
24+
))}
25+
</React.Profiler>
26+
);
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as React from 'react';
2+
import { createMuiTheme } from '@material-ui/core/styles';
3+
import { styleFunction } from '@material-ui/core/Box';
4+
import styledComponents, {
5+
ThemeProvider as StyledComponentsThemeProvider,
6+
} from 'styled-components';
7+
import { logReactMetrics } from '../utils';
8+
9+
const materialSystemTheme = createMuiTheme();
10+
const BoxStyleComponents = styledComponents('div')(styleFunction);
11+
12+
export default function BoxStyledComponents() {
13+
return (
14+
<React.Profiler id="box-styled-components" onRender={logReactMetrics}>
15+
{new Array(1000).fill().map(() => (
16+
<StyledComponentsThemeProvider theme={materialSystemTheme}>
17+
<BoxStyleComponents
18+
color="primary.main"
19+
bgcolor="background.paper"
20+
fontFamily="h6.fontFamily"
21+
fontSize={['h6.fontSize', 'h4.fontSize', 'h3.fontSize']}
22+
p={[2, 3, 4]}
23+
>
24+
styled-components
25+
</BoxStyleComponents>
26+
</StyledComponentsThemeProvider>
27+
))}
28+
</React.Profiler>
29+
);
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createMuiTheme } from '@material-ui/core/styles';
2+
import { styleFunction } from '@material-ui/core/Box';
3+
4+
const materialSystemTheme = createMuiTheme();
5+
6+
export default function MaterialUISystemAllInclusive() {
7+
styleFunction({
8+
theme: materialSystemTheme,
9+
color: 'primary.main',
10+
bgcolor: 'background.paper',
11+
fontFamily: 'h6.fontFamily',
12+
fontSize: ['h6.fontSize', 'h4.fontSize', 'h3.fontSize'],
13+
p: [2, 3, 4],
14+
});
15+
16+
return null;
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { palette } from '@material-ui/system';
2+
3+
export default function MaterialUISystemColors() {
4+
palette({
5+
theme: {},
6+
bgcolor: ['red', 'blue'],
7+
});
8+
9+
return null;
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createMuiTheme } from '@material-ui/core/styles';
2+
import { spacing, palette, typography, compose } from '@material-ui/system';
3+
4+
const materialSystem = compose(palette, spacing, typography);
5+
const materialSystemTheme = createMuiTheme();
6+
7+
export default function MaterialUISystemCompose() {
8+
materialSystem({
9+
theme: materialSystemTheme,
10+
color: 'primary.main',
11+
bgcolor: 'background.paper',
12+
fontFamily: 'h6.fontFamily',
13+
fontSize: ['h6.fontSize', 'h4.fontSize', 'h3.fontSize'],
14+
p: [2, 3, 4],
15+
});
16+
17+
return null;
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { spacing } from '@material-ui/system';
2+
3+
export default function MaterialUISystemSpaces() {
4+
spacing({
5+
theme: {},
6+
p: [1, 2, 3],
7+
});
8+
9+
return null;
10+
}

0 commit comments

Comments
 (0)