|
| 1 | +Let's open the `perf` project in our repo and run `npm install`. |
| 2 | + |
| 3 | +We're going to build a markdown previewer! On some laptops this can be pretty slow to parse and re-render to the DOM. On my laptop it's actually fast enough to get through it so we're going to introduce some artifical jank. Your computer may not need it. |
| 4 | + |
| 5 | +I left a long markdown file for you to use as a sample in markdownContent.js. I asked Claude to make some jokes for us. I'd call it a middling success. |
| 6 | + |
| 7 | +Make a new file called MarkdownPreview.jsx. Put this in there |
| 8 | + |
| 9 | +```javascript |
| 10 | +const JANK_DELAY = 100; |
| 11 | + |
| 12 | +export default function MarkdownPreview({ render, options }) { |
| 13 | + const expensiveRender = () => { |
| 14 | + const start = performance.now(); |
| 15 | + while (performance.now() - start < JANK_DELAY) {} |
| 16 | + return null; |
| 17 | + }; |
| 18 | + return ( |
| 19 | + <div> |
| 20 | + <h1>Last Render: {Date.now()}</h1> |
| 21 | + <div |
| 22 | + className="markdown-preview" |
| 23 | + dangerouslySetInnerHTML={{ __html: render(options.text) }} |
| 24 | + style={{ color: options.theme }} |
| 25 | + ></div> |
| 26 | + {expensiveRender()} |
| 27 | + </div> |
| 28 | + ); |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +- This is the artificial jank. It ties up the main thread so it'll run slower. Feel free to modify `JANK_DELAY`. Right now I have it delaying 100ms so you can see the jank more pronounced. |
| 33 | +- I'm also showing the time so can see how often the component runs. |
| 34 | +- `dangerouslySetInnerHTML` is fun. It just means you need to _really_ trust what you're putting in there. If you just put raw user generated content in there, they could drop a script tag in there and do a good ol' [XSS attack][xss]. In this case the user can only XSS themself so that's fine enough. |
| 35 | + |
| 36 | +Okay, let's make our App.jsx |
| 37 | + |
| 38 | +```javascript |
| 39 | +import { useEffect } from "react"; |
| 40 | +import { marked } from "marked"; |
| 41 | +import { useState } from "react"; |
| 42 | + |
| 43 | +import MarkdownPreview from "./MarkdownPreview"; |
| 44 | +import markdownContent from "./markdownContent"; |
| 45 | + |
| 46 | +export default function App() { |
| 47 | + const [text, setText] = useState(markdownContent); |
| 48 | + const [time, setTime] = useState(Date.now()); |
| 49 | + const [theme, setTheme] = useState("green"); |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + const interval = setInterval(() => { |
| 53 | + setTime(Date.now()); |
| 54 | + }, 1000); |
| 55 | + return () => clearInterval(interval); |
| 56 | + }, []); |
| 57 | + |
| 58 | + const options = { text, theme }; |
| 59 | + const render = (text) => marked.parse(text); |
| 60 | + |
| 61 | + return ( |
| 62 | + <div className="app"> |
| 63 | + <h1>Performance with React</h1> |
| 64 | + <h2>Current Time: {time}</h2> |
| 65 | + <label htmlFor={"theme"}> |
| 66 | + Choose a theme: |
| 67 | + <select value={theme} onChange={(e) => setTheme(e.target.value)}> |
| 68 | + <option value="green">Green</option> |
| 69 | + <option value="blue">Blue</option> |
| 70 | + <option value="red">Red</option> |
| 71 | + <option value="yellow">Yellow</option> |
| 72 | + </select> |
| 73 | + </label> |
| 74 | + <div className="markdown"> |
| 75 | + <textarea |
| 76 | + className="markdown-editor" |
| 77 | + value={text} |
| 78 | + onChange={(e) => setText(e.target.value)} |
| 79 | + ></textarea> |
| 80 | + <MarkdownPreview options={options} render={render} /> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ); |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Alright, go play with it now (you may need to mess with the JANK_DELAY as well as the interval of how often the interval runs). The scroll is probably either janky or it has a hard time re-rendering. Typing in it should hard as well. Also notice that the current render. Re-rendering the theme is tough too. |
| 88 | + |
| 89 | +So how can we fix at least the scroll portion, as well make the other two a little less painful (as they'll only re-renderingo once as opposed to continually.) |
| 90 | + |
| 91 | +[xss]: https://owasp.org/www-community/attacks/xss/ |
0 commit comments