<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Neal&#039;s Blog</title>
	<atom:link href="https://nealbuerger.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://nealbuerger.com</link>
	<description></description>
	<lastBuildDate>Sun, 02 May 2021 11:46:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.1</generator>
<site xmlns="com-wordpress:feed-additions:1">155939877</site>	<item>
		<title>Interview &#8211; React Optimization Question</title>
		<link>https://nealbuerger.com/2021/05/01/interview-react-optimization-question/</link>
		
		<dc:creator><![CDATA[Neal Bürger]]></dc:creator>
		<pubDate>Sat, 01 May 2021 20:34:32 +0000</pubDate>
				<category><![CDATA[Exercises]]></category>
		<category><![CDATA[Job Interview]]></category>
		<category><![CDATA[job interview]]></category>
		<category><![CDATA[react]]></category>
		<guid isPermaLink="false">https://nealbuerger.com/?p=276</guid>

					<description><![CDATA[Recently I ran into an interesting problem in a live coding session. The interviewer found a problem with my code [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Recently I ran into an interesting problem in a live coding session. The interviewer found a problem with my code and asked me to improve the code. Take a look at the following code. What do you think can be optimized?</p>



<pre class="EnlighterJSRAW" data-enlighter-language="typescript" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">const HeadLine: React.FC&lt;{ text: string }> = ({text}) => &lt;h1>{text}&lt;/h1>;
const TextBlock: React.FC&lt;{ text: string }> = ({text}) => &lt;p>{text}&lt;/p>;

export interface Item {
  type: "Headline" | "Block";
  text: string;
}

export const RenderBlock: React.FC&lt;{ block: Item }> = ({ block }) => {
  const { text, type } = block;
  const RenderMap = {
    Headline: () => &lt;HeadLine text={text} />,
    Block: () => &lt;TextBlock text={text} />,
  };

  return RenderMap[type]();
};</pre>



<h2>Solution</h2>



<p>When you use the RenderBlock component, the constant RenderMap is going to be recreated every time the component is used. You can extract the constant and put it into a higher scope to solve that problem.</p>



<p>The optimized code would look like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="typescript" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">const HeadLine: React.FC&lt;{ text: string }> = ({text}) => &lt;h1>{text}&lt;/h1>;
const TextBlock: React.FC&lt;{ text: string }> = ({text}) => &lt;p>{text}&lt;/p>;

export interface Item {
  type: "Headline" | "Block";
  text: string;
}

const RenderMap = {
    Headline: (text:string) => &lt;HeadLine text={text} />,
    Block: (text:string) => &lt;TextBlock text={text} />,
  };

export const RenderBlock: React.FC&lt;{ block: Item }> = ({ block }) => {
    const { text, type } = block;
    return RenderMap[type](text);
  };</pre>



<h2>How much did we improve the code?</h2>



<p>If we benchmark a similar piece of code with <a href="https://jsben.ch/yY9aT">js.bench</a> we can see a 15% performance improvement. Technically we can see a real-world improvement. More operations can be done in the same amount of time (390000ops vs 400000ops)</p>



<p>The downside is that the code is harder to read (it is not reading a single block but jumping up and down in the code) and harder to extend (if parameters change for example). </p>



<p>In a real-world environment, you probably will have a maximum of 100 instances of this component. By optimizing the code we probably will save only a couple of microseconds. </p>



<p>I would argue that this is a case of premature optimization. It will not have any noticeable benefit for the end-user, while at the same time make the developer experience worse. This is an issue that would be needed to be discussed with the team to further define the style of how the code is written in the codebase. </p>



<p>In the end, this is a coding interview, so there is no real debate. The interviewer is expecting a certain solution. Even if your solution is correct, it might not be correct because you did not match the values and expectations of the interviewer. &#8211; I probably should have answered: &#8220;Not everything that can be optimized should be optimized.&#8221;</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">276</post-id>	</item>
	</channel>
</rss>
