<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jharna Khatun</title>
    <description>The latest articles on DEV Community by Jharna Khatun (@jharna_khatun).</description>
    <link>https://dev.to/jharna_khatun</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2218136%2F0ff1542c-8aff-41d2-8032-fd1176d05ebf.png</url>
      <title>DEV Community: Jharna Khatun</title>
      <link>https://dev.to/jharna_khatun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jharna_khatun"/>
    <language>en</language>
    <item>
      <title>Dynamically create option in select element</title>
      <dc:creator>Jharna Khatun</dc:creator>
      <pubDate>Sun, 20 Oct 2024 12:38:18 +0000</pubDate>
      <link>https://dev.to/jharna_khatun/dynamically-create-option-in-select-element-d6c</link>
      <guid>https://dev.to/jharna_khatun/dynamically-create-option-in-select-element-d6c</guid>
      <description>&lt;h3&gt;
  
  
  There are two ways to create an option dynamically :
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1) Using the Option constructor and add() method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) Using the DOM methods&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1) Using the Option constructor and add() method :&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newOption&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Option&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Option Text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Option Value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;select&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;select&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newOption&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;2) Using the DOM methods :&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// create option using DOM&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newOption&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;option&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;optionText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createTextNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Option Text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// set option text&lt;/span&gt;
&lt;span class="nx"&gt;newOption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;optionText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// and option value&lt;/span&gt;
&lt;span class="nx"&gt;newOption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Option Value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;select&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;select&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newOption&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Some effective ways to create checkboxes</title>
      <dc:creator>Jharna Khatun</dc:creator>
      <pubDate>Sat, 19 Oct 2024 16:31:28 +0000</pubDate>
      <link>https://dev.to/jharna_khatun/some-effective-ways-to-create-checkboxes-5f1c</link>
      <guid>https://dev.to/jharna_khatun/some-effective-ways-to-create-checkboxes-5f1c</guid>
      <description>&lt;h2&gt;
  
  
  There are 3 ways to create checkbox :
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;By direct html code&lt;/li&gt;
&lt;li&gt;By JS code, create each element, attributes, content and appendChild child to  parent&lt;/li&gt;
&lt;li&gt;By JS code, with innerHTML and Template literal&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;By direct html code :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;
  &amp;lt;input type="checkbox" name="color" id="red"&amp;gt;
  &amp;lt;label for="red"&amp;gt;Red&amp;lt;/label&amp;gt;
 &amp;lt;/div&amp;gt;
 &amp;lt;div&amp;gt;
  &amp;lt;input type="checkbox" name="color" id="green"&amp;gt;
  &amp;lt;label for="green"&amp;gt;Green&amp;lt;/label&amp;gt;
 &amp;lt;/div&amp;gt;
 &amp;lt;div&amp;gt;
  &amp;lt;input type="checkbox" name="color" id="Blue"&amp;gt;
  &amp;lt;label for="Blue"&amp;gt;Blue&amp;lt;/label&amp;gt;
 &amp;lt;/div&amp;gt;
 &amp;lt;div&amp;gt;
  &amp;lt;input type="checkbox" name="color" id="yellow"&amp;gt;
  &amp;lt;label for="yellow"&amp;gt;Yellow&amp;lt;/label&amp;gt;
 &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;By JS code, create each element, attributes, content and appendChild child to  parent :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
    &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;

    &amp;lt;script&amp;gt;
      const root = document.getElementById("root");
      const colors = ["Red", "Green", "Blue", "Yellow"];
      colors.forEach((color) =&amp;gt; {
        // create id
        const id = color;

        // create label
        const label = document.createElement("label");
        label.setAttribute("for", id);

        // create checkbox input element
        const input = document.createElement("input");
        input.type = "checkbox";
        input.name = "color";
        input.id = id;
        input.value = color;

        // appendChild child to parent
        label.appendChild(input);
        label.appendChild(document.createTextNode(color));
        root.appendChild(label);
      });
    &amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;By JS code, with innerHTML and Template literal :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
    &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;

    &amp;lt;script&amp;gt;
      const root = document.getElementById("root");
      const colors = ["Red", "Green", "Blue", "Yellow"];
      const checkbox = colors.map((color)=&amp;gt;`&amp;lt;label for="${color}"&amp;gt;
        &amp;lt;input type="checkbox" name="color" id="${color}" value="${color}" &amp;gt;
        ${color}&amp;lt;/label&amp;gt;
      `
    ).join("");

    root.innerHTML = checkbox;
    &amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>checkbox</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Reflows and Repaints in Javascript</title>
      <dc:creator>Jharna Khatun</dc:creator>
      <pubDate>Fri, 18 Oct 2024 13:54:46 +0000</pubDate>
      <link>https://dev.to/jharna_khatun/reflows-and-repaints-in-javascript-2ep1</link>
      <guid>https://dev.to/jharna_khatun/reflows-and-repaints-in-javascript-2ep1</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;What are the best practices for optimizing CSS to reduce unnecessary reflows and repaints, especially in large-scale applications?&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Reflow&lt;/strong&gt; (Layout Recalculation):
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;reflow&lt;/strong&gt; (also called &lt;strong&gt;layout&lt;/strong&gt; or &lt;strong&gt;re-layout&lt;/strong&gt;) occurs when the browser recalculates the position, size, and layout of elements on the page. This process happens every time the layout of the page changes such as when elements are added, removed, resized, or their visibility changes. It’s a more complex and time-consuming operation&lt;/p&gt;

&lt;h3&gt;
  
  
  Example :
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"box"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width: 100px; height: 100px; background-color: blue;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;box&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;box&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Triggering a reflow by changing width and height&lt;/span&gt;
&lt;span class="nx"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;200px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;200px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Triggering a repaint by changing the background color&lt;/span&gt;
&lt;span class="nx"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  When does a reflow happen?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Adding, removing, or modifying DOM elements (e.g., &lt;code&gt;appendChild&lt;/code&gt;, &lt;code&gt;removeChild&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Changing the layout by adjusting CSS styles like &lt;code&gt;width&lt;/code&gt;, &lt;code&gt;height&lt;/code&gt;, &lt;code&gt;margin&lt;/code&gt;, &lt;code&gt;padding&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;Resizing the window or changing the size of elements.&lt;/li&gt;
&lt;li&gt;Changing the font size or font properties.&lt;/li&gt;
&lt;li&gt;Using methods like &lt;code&gt;offsetWidth&lt;/code&gt;, &lt;code&gt;offsetHeight&lt;/code&gt;, &lt;code&gt;scrollTop&lt;/code&gt;, &lt;code&gt;getBoundingClientRect()&lt;/code&gt; because they force the browser to recalculate layout.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How reflow works:
&lt;/h3&gt;

&lt;p&gt;When you change something that affects the layout of the page, the browser has to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Recalculate the positions and dimensions&lt;/strong&gt; of all elements affected by the change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rebuild the layout tree&lt;/strong&gt;, which is the internal representation of how the elements are laid out.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If many elements are affected by a single change, the reflow can be costly and slow down the performance of your site.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdanj1e3h7xq5rf9agy12.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdanj1e3h7xq5rf9agy12.png" alt="Image description" width="512" height="517"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. Repaint&lt;/strong&gt; (Visual Update)
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;repaint&lt;/strong&gt; (or &lt;strong&gt;redraw&lt;/strong&gt;) occurs when visual properties of an element change but the layout doesn’t. It’s less expensive than a reflow because it only requires updating the appearance of elements without recalculating their position or layout. Repaints happen after the layout is recalculated (in cases where both are needed) or when non-layout-affecting properties are changed, like color or visibility.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example :
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"box"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width: 100px; height: 100px; background-color: blue;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;// Triggering a repaint by changing the background color&lt;/span&gt;
&lt;span class="nx"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  When does a repaint happen?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Changing the &lt;code&gt;background-color&lt;/code&gt;, &lt;code&gt;border-color&lt;/code&gt;, or &lt;code&gt;visibility&lt;/code&gt; properties.&lt;/li&gt;
&lt;li&gt;Changing the &lt;code&gt;box-shadow&lt;/code&gt;, &lt;code&gt;outline&lt;/code&gt;, or &lt;code&gt;color&lt;/code&gt; of an element.&lt;/li&gt;
&lt;li&gt;Updating opacity (&lt;code&gt;opacity&lt;/code&gt;), transforms (&lt;code&gt;transform&lt;/code&gt;), or z-index.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A repaint doesn’t involve recalculating layout, so it’s faster than reflow, but it still requires redrawing parts of the page, which takes some time.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Rendering Pipeline
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;DOM Construction&lt;/strong&gt;: The browser parses HTML to build the &lt;strong&gt;DOM&lt;/strong&gt; (Document Object Model) tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CSSOM Construction&lt;/strong&gt;: CSS is parsed to build the &lt;strong&gt;CSSOM&lt;/strong&gt; (CSS Object Model) tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Render Tree Construction&lt;/strong&gt;: The DOM and CSSOM are combined to create the &lt;strong&gt;render tree&lt;/strong&gt;, which contains the visual information for each visible element.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Layout (Reflow)&lt;/strong&gt;: The browser calculates the position and size of each visible element in the render tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Paint&lt;/strong&gt;: The browser fills in pixels based on the visual properties like colors, borders, shadows, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composite&lt;/strong&gt;: The browser composes the different painted layers (for complex elements like animations, 3D transforms, etc.) and displays them on the screen.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Performance Impact
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reflow&lt;/strong&gt;: Expensive in terms of performance, especially if it affects large parts of the page or is triggered repeatedly (e.g., in a loop or on resize). It affects the layout of the page and requires recalculating positions and sizes of elements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repaint&lt;/strong&gt;: Less expensive than a reflow but still impacts performance, especially if many elements need to be repainted frequently.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How to Optimize Reflows and Repaints
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Minimize DOM manipulation&lt;/strong&gt; :  Use techniques like &lt;strong&gt;&lt;a href="https://dev.to/jharna_khatun/what-is-a-batch-dom-update-and-why-is-it-useful-d3k"&gt;batching DOM updates&lt;/a&gt;&lt;/strong&gt; (as mentioned earlier) or &lt;strong&gt;DocumentFragment&lt;/strong&gt; to make multiple changes at once, instead of one by one.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Avoid layout thrashing&lt;/strong&gt; : If you read a layout property (e.g., &lt;code&gt;offsetHeight&lt;/code&gt;) and immediately write (change the layout) in the same cycle, it forces a reflow, known as layout thrashing. To avoid this, separate reading and writing DOM properties in different steps.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offsetHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;px&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use CSS classes&lt;/strong&gt; : Instead of modifying individual styles, use CSS classes to make changes. The browser handles class switching more efficiently.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;classList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newClass&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduce complexity of CSS&lt;/strong&gt;:  Avoid deeply nested elements and overly complex CSS rules that can trigger reflows.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;visibility: hidden&lt;/code&gt; instead of &lt;code&gt;display: none&lt;/code&gt;&lt;/strong&gt; when you just want to hide an element without affecting layout. &lt;code&gt;display: none&lt;/code&gt; triggers a reflow, while &lt;code&gt;visibility: hidden&lt;/code&gt; only triggers a repaint.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reflows&lt;/strong&gt; involve recalculating the layout of the page and are more costly in terms of performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repaints&lt;/strong&gt; update the visual appearance without affecting layout and are less costly.&lt;/li&gt;
&lt;li&gt;Minimizing both helps keep your site responsive and fast, improving user experience.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>jsdom</category>
      <category>performance</category>
      <category>web</category>
    </item>
    <item>
      <title>What is a Batch DOM update and Why is it useful?</title>
      <dc:creator>Jharna Khatun</dc:creator>
      <pubDate>Fri, 18 Oct 2024 13:01:17 +0000</pubDate>
      <link>https://dev.to/jharna_khatun/what-is-a-batch-dom-update-and-why-is-it-useful-d3k</link>
      <guid>https://dev.to/jharna_khatun/what-is-a-batch-dom-update-and-why-is-it-useful-d3k</guid>
      <description>&lt;h2&gt;
  
  
  Batching DOM updates :
&lt;/h2&gt;

&lt;p&gt;Batching DOM updates refers to making multiple changes to the DOM in a way that reduces the number of &lt;strong&gt;reflows&lt;/strong&gt; and &lt;strong&gt;repaints&lt;/strong&gt;, which are costly operations for the browser. A Batch DOM update is when you make multiple changes to a webpage's structure (the DOM) all at once, instead of one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is it useful?
&lt;/h2&gt;

&lt;p&gt;Making changes to the DOM one at a time can slow down your webpage because the browser has to keep stopping, recalculating positions, and redrawing the page for every single change. By &lt;strong&gt;batching&lt;/strong&gt; updates, you combine all the changes and apply them in one go, making your webpage faster and smoother.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario:
&lt;/h2&gt;

&lt;p&gt;Imagine you’re moving furniture around a room. If you move one chair, then wait for everyone to notice, then move a table, and wait again, it will take forever. But if you move everything at once and show the final result, the process will be quicker and less disruptive.&lt;/p&gt;

&lt;p&gt;In coding terms, instead of updating the DOM every time you add or change something, you collect all your changes and apply them together, which is much faster.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example:
&lt;/h2&gt;

&lt;p&gt;Instead of adding items to a list one by one (slow):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Updating DOM one by one (slow)
const list = document.getElementById('myList');
const newItem = document.createElement('li');
newItem.textContent = "Item 1";
list.appendChild(newItem);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can collect all the items in a "batch" and add them all at once (fast):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Batch DOM update (fast)
const list = document.getElementById('myList');
const fragment = document.createDocumentFragment();

for (let i = 1; i &amp;lt;= 5; i++) {
    const newItem = document.createElement('li');
    newItem.textContent = `Item ${i}`;
    fragment.appendChild(newItem);
}

list.appendChild(fragment);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, the browser only updates the page once after all the items are ready, making it faster and more efficient!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>jsdom</category>
    </item>
  </channel>
</rss>
