<?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>Forem: Umair Syed</title>
    <description>The latest articles on Forem by Umair Syed (@umairian).</description>
    <link>https://forem.com/umairian</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%2F2047677%2F735a32d7-ccf5-407e-a81d-30863fd2929e.jpg</url>
      <title>Forem: Umair Syed</title>
      <link>https://forem.com/umairian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/umairian"/>
    <language>en</language>
    <item>
      <title>Fixing "Connection Refused" Errors with Serverless Offline</title>
      <dc:creator>Umair Syed</dc:creator>
      <pubDate>Tue, 28 Jan 2025 10:44:36 +0000</pubDate>
      <link>https://forem.com/umairian/fixing-connection-refused-errors-with-serverless-offline-nld</link>
      <guid>https://forem.com/umairian/fixing-connection-refused-errors-with-serverless-offline-nld</guid>
      <description>&lt;p&gt;If you've ever encountered the frustrating &lt;code&gt;ECONNREFUSED&lt;/code&gt; error while working with &lt;strong&gt;Serverless Offline&lt;/strong&gt;, you're not alone. I was getting this error when I tried to run e2e tests with cypress. The frontend was working fine and was able to hit the serverless offline backend but in cypress tests, it was giving refused connection errors. In this blog, I'll walk you through how I solved this problem and got my serverless backend up and running locally.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Problem&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;While running my e2e cypress tests locally, I kept running into the following error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: connect ECONNREFUSED 127.0.0.1:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This error indicated that my application (or Cypress tests) couldn't connect to the backend running on &lt;code&gt;localhost:8080&lt;/code&gt;. Surprisingly, the backend worked fine when tested manually with &lt;code&gt;curl&lt;/code&gt; or Postman or browser, but it failed when accessed via Cypress or other tools.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Solution&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;After some debugging, I discovered two key changes that resolved the issue:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;1. Change the Port&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;By default, &lt;code&gt;sls offline&lt;/code&gt; runs on port &lt;code&gt;8080&lt;/code&gt;. However, this port might already be in use by another process on your machine. To fix this, I updated the &lt;code&gt;serverless.yml&lt;/code&gt; file to use a different port (e.g., &lt;code&gt;8000&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;custom&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;serverless-offline&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;httpPort&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;strong&gt;2. Set the Host Explicitly&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Sometimes, &lt;code&gt;sls offline&lt;/code&gt; binds to &lt;code&gt;localhost&lt;/code&gt; by default, which can cause issues in certain environments. To ensure the server is accessible from all network interfaces, I explicitly set the host to &lt;code&gt;0.0.0.0&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;custom&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;serverless-offline&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;httpPort&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8000&lt;/span&gt;
    &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;0.0.0.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After making these changes, I restarted &lt;code&gt;sls offline&lt;/code&gt;, and there you go! The backend was now accessible at &lt;code&gt;http://localhost:8000&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why This Works&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Changing the Port&lt;/strong&gt;: If port &lt;code&gt;8080&lt;/code&gt; is already in use, switching to a different port (e.g., &lt;code&gt;8000&lt;/code&gt;) ensures there’s no conflict.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Setting the Host to &lt;code&gt;0.0.0.0&lt;/code&gt;&lt;/strong&gt;: Binding to &lt;code&gt;0.0.0.0&lt;/code&gt; makes the server accessible from any network interface, not just &lt;code&gt;localhost&lt;/code&gt;. This is especially useful when working with tools like Cypress or Docker.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Steps to Reproduce the Fix&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Update your &lt;code&gt;serverless.yml&lt;/code&gt; file:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;   &lt;span class="na"&gt;custom&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
     &lt;span class="na"&gt;serverless-offline&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
       &lt;span class="na"&gt;httpPort&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8000&lt;/span&gt;
       &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;0.0.0.0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Restart &lt;code&gt;sls offline&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   sls offline
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Test the backend using &lt;code&gt;curl&lt;/code&gt; or your application:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://localhost:8000/prod/signIn &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
     &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{"email": "test@example.com", "password": "your-password"}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also resolve the issue via terminal command by running &lt;code&gt;sls offline&lt;/code&gt; with specific options directly. This can be done in one line like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sls offline start &lt;span class="nt"&gt;--httpPort&lt;/span&gt; 8000 &lt;span class="nt"&gt;--host&lt;/span&gt; 0.0.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will start your serverless offline environment on port &lt;code&gt;8000&lt;/code&gt; and bind it to &lt;code&gt;0.0.0.0&lt;/code&gt;, making the backend accessible from all network interfaces without the need to modify the &lt;code&gt;serverless.yml&lt;/code&gt; file manually. It's a quick and efficient alternative for testing.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Bonus Tip: Debugging with Cypress&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you're using Cypress for end-to-end testing and still encountering issues, try hitting the backend directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:8000/api/signIn&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Replace with your backend URL&lt;/span&gt;
        &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cypress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TEST_USER_EMAIL&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Cypress&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TEST_USER_PASSWORD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;token&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exist&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>serverless</category>
      <category>lambda</category>
      <category>docker</category>
      <category>cypress</category>
    </item>
    <item>
      <title>Introduction to LLMs: A Useful Handbook</title>
      <dc:creator>Umair Syed</dc:creator>
      <pubDate>Thu, 03 Oct 2024 06:07:25 +0000</pubDate>
      <link>https://forem.com/umairian/introduction-to-llms-a-useful-handbook-2po</link>
      <guid>https://forem.com/umairian/introduction-to-llms-a-useful-handbook-2po</guid>
      <description>&lt;p&gt;In the rapidly advancing field of Artificial Intelligence (AI), understanding the foundations is key, especially when dealing with Large Language Models (LLMs). This guide aims to simplify complex topics for beginners, taking you through essential concepts like neural networks, natural language processing (NLP), and LLMs. We'll explore how LLMs are built, trained, and keywords related to LLMs, as well as the challenges they face, like biases and hallucinations.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Are Neural Networks?
&lt;/h3&gt;

&lt;p&gt;A neural network is a machine learning model that makes decisions like the human brain, by mimicking how biological neurons work together to recognize patterns, evaluate choices, and reach conclusions. Neural networks are the backbone of any AI model, including LLMs. As the backbone of AI models, including LLMs, neural networks are organized into layers that process data and learn from it. The neurons in a neural network are arranged in layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input Layer:&lt;/strong&gt; Where the data first enters the model.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hidden Layers:&lt;/strong&gt; Where computations happen, allowing the network to learn patterns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output Layer:&lt;/strong&gt; Where the final prediction or decision is made.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, a neural network can be trained to recognize images of cats. It processes an image through layers of neurons, enabling it to identify a cat among different shapes or objects.&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%2Fi0v4y8ucp4iejb1zekkw.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%2Fi0v4y8ucp4iejb1zekkw.png" alt="neural networks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is Natural Language Processing (NLP)?
&lt;/h3&gt;

&lt;p&gt;Natural language processing (NLP) is a computer program's ability to understand human language, both spoken and written. NLP is the field that focuses on enabling machines to understand and generate human language. NLP powers everything from chatbots to voice assistants, translating human language into something a machine can process. It involves tasks like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tokenization:&lt;/strong&gt; Breaking text into smaller components (e.g., words or subwords).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parsing:&lt;/strong&gt; Understanding the structure of sentences.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sentiment Analysis:&lt;/strong&gt; Determining whether a piece of text is positive, negative, or neutral.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without NLP, LLMs wouldn't be able to grasp the nuances of human language.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Are Large Language Models (LLMs)?
&lt;/h3&gt;

&lt;p&gt;Large Language Models (LLMs) are advanced neural networks designed to understand and generate human language. They are trained on vast amounts of text data, allowing them to learn patterns, context, and nuances of language. LLMs can perform various tasks, such as answering questions, writing essays, translating languages, and engaging in conversations. Their primary goal is to create human-like text that captures the intricacies of natural language.&lt;/p&gt;

&lt;p&gt;The core idea behind LLMs is simple: predict the next word in a sentence. For example, if you input "The sun rises in the...", the LLM should predict "east." While this may seem basic, this task leads to the development of complex, emergent abilities like text generation, reasoning, and even creativity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key LLM Terminologies
&lt;/h3&gt;

&lt;h3&gt;
  
  
  The Transformer
&lt;/h3&gt;

&lt;p&gt;A Transformer is a type of neural network architecture that revolutionized natural language processing (NLP) by enabling models to handle sequences of data more efficiently. The Transformer architecture was introduced in the groundbreaking paper &lt;code&gt;Attention Is All You Need&lt;/code&gt;. Traditional models like Recurrent Neural Networks (RNNs) processed sequential data and maintained an internal state, allowing them to handle sequences like sentences. However, they struggled with long sequences due to issues like the vanishing gradient problem, because over time, they would forget earlier information. This happened because the adjustments made to improve the model became too small to have any real impact.&lt;/p&gt;

&lt;p&gt;The Transformer addressed these challenges by using a mechanism called attention, which allowed the model to focus on different parts of a sentence or document more effectively, regardless of their position. This innovation laid the foundation of ground breaking models like GPT-4, Claude, and LLaMA.&lt;/p&gt;

&lt;p&gt;The architecture was first designed as an encoder-decoder framework. In this setup, the encoder processes input text, picking out important parts and creating a representation of it. The decoder then transforms this representation back into readable text. This approach is useful for tasks like summarization, where the decoder creates summaries based on the input passed to the encoder. The encoder and decoder can work together or separately, offering flexibility for various tasks. Some models only use the encoder to turn text into a vector, while others rely on just the decoder, which is the foundation of large language models.&lt;/p&gt;

&lt;h3&gt;
  
  
  Language Modeling
&lt;/h3&gt;

&lt;p&gt;Language modeling refers to the process of teaching LLMs to understand the probability distribution of words in a language. This allows models to predict the most likely next word in a sentence, a critical task in generating coherent text. The ability to generate coherent and contextually appropriate text is crucial in many applications, such as text summarization, translation, or conversational agents.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tokenization
&lt;/h3&gt;

&lt;p&gt;Tokenization is the first step when working with large language models (LLMs). It means breaking down a sentence into smaller parts, called tokens. These tokens can be anything from individual letters to whole words depending on the model, and how they're split can affect how well the model works. &lt;/p&gt;

&lt;p&gt;For example, consider the sentence: &lt;code&gt;The developer’s favorite machine.&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we split the text by spaces, we get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;developer&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;favorite&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;machine.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, punctuation like the apostrophe in &lt;code&gt;developer’s&lt;/code&gt; and the period at the end of &lt;code&gt;machine.&lt;/code&gt; stays attached to the words. But we can also split the sentence based on spaces and punctuation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;developer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;favorite&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;machine&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The way text is split into tokens depends on the model, and many advanced models use methods like &lt;strong&gt;subword tokenization&lt;/strong&gt;. This breaks words into smaller, meaningful parts. For example, the sentence &lt;code&gt;It's raining&lt;/code&gt; can be split as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;It&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"'"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;s&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rain&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ing&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;raining&lt;/code&gt; is broken into &lt;code&gt;rain&lt;/code&gt; and &lt;code&gt;ing&lt;/code&gt;, which helps the model understand the structure of words. By splitting words into their base forms and endings (like &lt;code&gt;rain&lt;/code&gt; and &lt;code&gt;ing&lt;/code&gt; for &lt;code&gt;raining&lt;/code&gt;), the model can learn the meaning more effectively without needing to store different versions of every word.&lt;/p&gt;

&lt;p&gt;During tokenization, the text is scanned, and each token is assigned a unique ID in a dictionary. This allows the model to quickly refer to the dictionary when processing the text, making the input easier to understand and work with.&lt;/p&gt;

&lt;h3&gt;
  
  
  Embeddings
&lt;/h3&gt;

&lt;p&gt;After tokenization, the next step is to convert these tokens into something a computer can work with — which is done using &lt;strong&gt;embeddings&lt;/strong&gt;. Embeddings are a way to represent tokens (words or parts of words) as numbers that the computer can understand. These numbers help the model recognize relationships between words and their context.&lt;/p&gt;

&lt;p&gt;For example, let's say we have the words &lt;code&gt;happy&lt;/code&gt; and &lt;code&gt;joyful&lt;/code&gt;. The model assigns each word a set of numbers (its embedding) that captures its meaning. If two words are similar, like &lt;code&gt;happy&lt;/code&gt; and &lt;code&gt;joyful&lt;/code&gt;, their numbers will be close together, even though the words are different.&lt;/p&gt;

&lt;p&gt;At first, the model assigns random numbers to each token. But as the model trains—by reading and learning from large amounts of text—it adjusts those numbers. The goal is for tokens with similar meanings to have similar sets of numbers, helping the model understand the connections between them. &lt;/p&gt;

&lt;p&gt;Although it may sound complicated, embeddings are just lists of numbers that allow the model to store and process information efficiently. Using these numbers (or vectors) makes it easier for the model to understand how tokens relate to one another.&lt;/p&gt;

&lt;p&gt;Let's look at a simple example of how embeddings work:&lt;br&gt;
Imagine we have three words: &lt;code&gt;cat&lt;/code&gt;, &lt;code&gt;dog&lt;/code&gt;, and &lt;code&gt;car&lt;/code&gt;. The model will assign each word a set of numbers, like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cat&lt;/code&gt; → [1.2, 0.5]&lt;br&gt;
&lt;code&gt;dog&lt;/code&gt; → [1.1, 0.6]&lt;br&gt;
&lt;code&gt;car&lt;/code&gt; → [4.0, 3.5]&lt;br&gt;
Here, &lt;code&gt;cat&lt;/code&gt; and &lt;code&gt;dog&lt;/code&gt; have similar numbers because they are both animals, so their meanings are related. On the other hand, "car" has very different numbers because it’s a vehicle, not an animal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Training and Fine-Tuning
&lt;/h3&gt;

&lt;p&gt;Large language models (LLMs) are trained by reading massive amounts of text to learn how to predict the next word in a sentence. The model's goal is to adjust its internal settings to improve the chances of making accurate predictions based on the patterns it observes in the text. Initially, LLMs are trained on general datasets from the internet, like &lt;strong&gt;The Pile&lt;/strong&gt; or &lt;strong&gt;CommonCrawl&lt;/strong&gt;, which contain a wide variety of topics. For specialized knowledge, the model might also be trained on focused datasets, such as &lt;strong&gt;Reddit Posts&lt;/strong&gt;, which help it learn specific areas like programming.&lt;/p&gt;

&lt;p&gt;This initial training phase is called &lt;strong&gt;pre-training&lt;/strong&gt;, where the model learns to understand language overall. During this phase, the model’s internal &lt;code&gt;weights&lt;/code&gt; (its settings) are adjusted to help it predict the next word more accurately based on the training data.&lt;/p&gt;

&lt;p&gt;Once pre-training is done, the model usually undergoes a second phase called &lt;strong&gt;fine-tuning&lt;/strong&gt;. In fine-tuning, the model is trained on smaller datasets focused on specific tasks or domains, like medical text or financial reports. This helps the model apply what it learned during pre-training to perform better on specific tasks, such as translating text or answering questions about a particular field.&lt;/p&gt;

&lt;p&gt;For advanced models like GPT-4, fine-tuning requires complex techniques and even larger amounts of data to achieve their impressive performance levels.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prediction
&lt;/h3&gt;

&lt;p&gt;After training or fine-tuning, the model can create text by predicting the next word in a sentence (or next token to be precise). It does this by analyzing the input and giving each possible next token a score based on how likely it is to come next. The token with the highest score is chosen, and this process repeats for each new token. This way, the model can generate sentences of any length, but it’s important to remember that the model can only handle a certain amount of text at a time as input, known as its &lt;strong&gt;context size&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Context Size
&lt;/h3&gt;

&lt;p&gt;The context size, or context window, is a crucial aspect of LLMs. It refers to the maximum number of tokens the model can process in a single request. It determines how much information the model can handle in a single go, which impacts how well it performs and the quality of its output.&lt;/p&gt;

&lt;p&gt;Different models have different context sizes. For instance, OpenAI’s gpt-3.5-turbo-16k model can handle up to 16,000 tokens (which are parts of words or words themselves). Smaller models might manage only 1,000 tokens, while bigger ones like GPT-4-0125-preview can process up to 128,000 tokens. This limit affects how much text the model can generate at one time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scaling Laws
&lt;/h3&gt;

&lt;p&gt;Scaling laws explain how a language model's performance is affected by different factors, such as the number of parameters, the size of the training dataset, the computing power available, and the model's design. These laws, discussed in the &lt;em&gt;Chinchilla&lt;/em&gt; paper, help us understand how to best use resources to train models effectively. They also offer insights into optimizing performance. According to Scaling laws, The following elements determine a language model’s performance:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Number of Parameters (N):&lt;/strong&gt; Parameters are like tiny parts of the model’s &lt;code&gt;brain&lt;/code&gt; that help it learn. When the model reads data, it adjusts these parameters to get better at understanding patterns. The more parameters the model has, the smarter it becomes, meaning it can pick up on more complex and detailed patterns in the data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Training Dataset Size (D):&lt;/strong&gt; The training dataset is the collection of text or data the model learns from. The bigger the training dataset, the more the model can learn and recognize patterns in different texts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FLOPs (Floating Point Operations Per Second):&lt;/strong&gt; This term refers to the amount of computing power needed to train the model. It measures how fast the model can process data and perform calculations during training. More FLOPs mean the model can handle more complex tasks but also requires more computational resources to do so.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Emergent Abilities in LLMs
&lt;/h3&gt;

&lt;p&gt;As LLMs grow in size and complexity, they start exhibiting &lt;strong&gt;emergent abilities&lt;/strong&gt; that were not explicitly programmed into them. For example, GPT-4 can summarize long texts or even perform basic arithmetic without being specifically trained for those tasks. These abilities emerge because the model learns so much about language and data during training.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prompts
&lt;/h3&gt;

&lt;p&gt;Prompts are the instructions you give to LLMs to generate a desired output. Designing the right prompt can significantly improve the quality of the generated text. For example:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Use Clear Language:
&lt;/h4&gt;

&lt;p&gt;Be specific in your prompts to get better results.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Less Clear:&lt;/strong&gt; Write about Allama Iqbal.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More Clear:&lt;/strong&gt; Write a 500-word article on the great poet of sub-continent Allama Iqbal. &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Provide Enough Context:
&lt;/h4&gt;

&lt;p&gt;Context helps the model know what you want.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Less Context:&lt;/strong&gt; Write a story. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More Context:&lt;/strong&gt; Write a short story about a baby girl lost in the woods with happy ending.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Try Different Variations:
&lt;/h4&gt;

&lt;p&gt;Experiment with different prompt styles to see what works best.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Original:&lt;/strong&gt; Write a blog post about the benefits of programming.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variation 1:&lt;/strong&gt; Write a 1000-word blog post on the mental and financial benefits of regularly practicing programming. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variation 2:&lt;/strong&gt; Create an engaging blog post highlighting the top 10 benefits of programming.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. Review Outputs:
&lt;/h4&gt;

&lt;p&gt;Always check the automated responses for accuracy before sharing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hallucinations
&lt;/h3&gt;

&lt;p&gt;Hallucinations occur when LLMs generate content that is factually incorrect or nonsensical. For instance, an LLM might state that "The capital of Australia is Sydney," when the correct answer is Canberra. This happens because the model is focused on generating likely text based on its training, not verifying facts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Biases
&lt;/h3&gt;

&lt;p&gt;Bias in LLMs arises when the training data reflects cultural, gender, or racial biases. For example, if a model is trained predominantly on English text from Western sources, it may produce biased outputs that favor Western perspectives. Efforts are being made to minimize these biases, but they remain a critical challenge in the field.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>machinelearning</category>
      <category>basic</category>
    </item>
    <item>
      <title>The Journey of DNS Resolution: How the Internet Finds Websites for You</title>
      <dc:creator>Umair Syed</dc:creator>
      <pubDate>Tue, 01 Oct 2024 02:42:04 +0000</pubDate>
      <link>https://forem.com/umairian/the-journey-of-dns-resolution-how-the-internet-finds-websites-for-you-1naf</link>
      <guid>https://forem.com/umairian/the-journey-of-dns-resolution-how-the-internet-finds-websites-for-you-1naf</guid>
      <description>&lt;p&gt;Imagine you’re in a big city, trying to visit a building. Instead of using an address like &lt;code&gt;123 Main Street&lt;/code&gt; you just remember the name of the building, like &lt;code&gt;Google Headquarters&lt;/code&gt; To find this building, you need someone to convert the name into an actual address. In the same way, websites have IP addresses (like &lt;code&gt;192.168.1.1&lt;/code&gt;) that are needed for your computer to find them. Every device on the internet, including websites, has its own unique IP address. Without these addresses, your computer wouldn’t know where to send or receive information, and websites wouldn’t be able to load. That’s exactly what &lt;strong&gt;DNS (Domain Name System)&lt;/strong&gt; does for websites—it turns easy-to-remember names into IP addresses so your computer can find the right website. This process is called DNS resolution, where the system works behind the scenes to translate a website's name (like "google.com") into the numerical IP address needed to load the site on your browser.&lt;/p&gt;

&lt;p&gt;Let’s learn how DNS works step-by-step, so by the end, you’ll know exactly what happens when you type a website name and hit "Enter."&lt;/p&gt;

&lt;h3&gt;
  
  
  How DNS Resolution Works
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;You Type a Website Name (URL)&lt;/strong&gt;:&lt;br&gt;&lt;br&gt;
   You open your browser and type in “example.com” Before the page can load, your computer needs to know the IP address for "example.com"—the exact location on the internet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: The Recursive DNS Resolver&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
   The &lt;strong&gt;recursive DNS resolver&lt;/strong&gt; is responsible for finding the correct IP address for the website you want to visit. It does this by asking different servers until it gets an answer.&lt;br&gt;
Your computer asks for help from a &lt;strong&gt;recursive DNS resolver&lt;/strong&gt;. This is like your internet assistant, and it’s usually provided by your internet service provider (ISP). If the resolver has seen the website before, it may have the IP address stored already. If not, it begins asking other servers to help find the IP address.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: The Root Name Server&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
   The &lt;strong&gt;root name server&lt;/strong&gt; is the first step in the DNS process. It works like a guide, directing the resolver to the right place based on the website's ending, like &lt;code&gt;.com&lt;/code&gt; or &lt;code&gt;.org&lt;/code&gt;.&lt;br&gt;
The recursive resolver doesn’t know the IP address itself, so its first stop is the &lt;strong&gt;root name server&lt;/strong&gt;. Think of the root server as a giant directory that knows where to find information for any website in the world. However, the root server doesn’t know the exact address for "example.com." Instead, it points the resolver to the next step: the &lt;strong&gt;Top-Level Domain (TLD) server&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: The TLD Name Server&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
   The &lt;strong&gt;TLD name server&lt;/strong&gt; is responsible for handling domain names that end with specific extensions, like ".com" or ".org." It points the resolver to the authoritative name server for that particular domain.&lt;br&gt;
The recursive resolver goes to the &lt;strong&gt;Top-Level Domain (TLD) server&lt;/strong&gt;. TLD servers are organized by the end of the website’s name, such as “.com,” “.org,” or “.net.” Since you are looking for "example.com", the resolver goes to the &lt;strong&gt;.com TLD server&lt;/strong&gt;. This server knows which &lt;strong&gt;authoritative name server&lt;/strong&gt; to ask for the exact IP address.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: The Authoritative Name Server&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
   The &lt;strong&gt;authoritative name server&lt;/strong&gt; holds all the information about a domain, including its IP address. It gives the resolver the final answer it needs to direct you to the right website.&lt;br&gt;
Finally, the recursive resolver arrives at the &lt;strong&gt;authoritative name server&lt;/strong&gt;. This server holds the &lt;strong&gt;DNS records&lt;/strong&gt; for "example.com". DNS records are like stored information about your domain—things like its IP address, mail server details, and more. The authoritative server looks up the IP address for "example.com" and gives it to the resolver.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How Your Domain's DNS Records Are Accessed&lt;/strong&gt;:
 When you register a domain and set up its DNS records, you are storing these records in the authoritative name server for that domain. So, if you own “mywebsite.com”, the IP address and other important details are saved in the authoritative server. Whenever someone tries to visit “mywebsite.com”, their recursive resolver will eventually ask your authoritative server for the IP address.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 5: The IP Address Is Returned&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
   Now that the recursive resolver has the correct IP address, it sends it back to your computer. Your browser uses this address to connect to the website, and "example.com" loads on your screen.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  DNS Caching: Speeding Things Up
&lt;/h3&gt;

&lt;p&gt;You might think that going through all these steps every time would take a long time, but in reality, it happens very quickly—usually in milliseconds. This is because of something called &lt;strong&gt;DNS caching&lt;/strong&gt;. Your computer, browser, and even the recursive resolver can store the IP addresses of websites for a certain amount of time. This means they don’t have to go through the whole process each time you visit a website. Instead, they can skip straight to the answer.&lt;/p&gt;

&lt;h3&gt;
  
  
  How your DNS records are pushed to Authoritative Name Server?
&lt;/h3&gt;

&lt;p&gt;When you set up DNS records for your domain (such as the IP address, email server details, etc.), the process typically involves using your &lt;strong&gt;domain registrar&lt;/strong&gt; or a &lt;strong&gt;DNS hosting provider&lt;/strong&gt;. Here's how your DNS records are pushed to the &lt;strong&gt;authoritative name server&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Register Your Domain&lt;/strong&gt;: The first step is to buy a domain (like "example.com") from a domain registrar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Set Up DNS Records&lt;/strong&gt;: Most domain registrars provide a control panel where you can manage your DNS records, or you can use a &lt;strong&gt;DNS hosting provider&lt;/strong&gt; like Cloudflare or AWS Route 53 to manage your records. These records include details like the IP address (A record) of your website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Push to Authoritative Servers&lt;/strong&gt;: Once you save your DNS records with the registrar or DNS hosting service, they automatically push these records to their &lt;strong&gt;authoritative name servers&lt;/strong&gt;. These are the servers that answer DNS queries for your domain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Propagation&lt;/strong&gt;: After you update or set up new DNS records, it may take some time for the changes to spread across the internet. This is called &lt;strong&gt;DNS propagation&lt;/strong&gt;, where all the DNS servers around the world update their cached information to reflect your changes.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Wrapping It All Up
&lt;/h3&gt;

&lt;p&gt;DNS resolution might seem like a lot of steps, but it’s a fast and efficient process that happens every time you visit a website. Whether you’re typing in "google.com" or "mywebsite.com", the DNS works behind the scenes to find the correct IP address, so your browser knows where to go.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>devops</category>
      <category>cloud</category>
      <category>backend</category>
    </item>
    <item>
      <title>From HTML to Pixels: The Magic Behind Web Rendering</title>
      <dc:creator>Umair Syed</dc:creator>
      <pubDate>Mon, 30 Sep 2024 03:30:59 +0000</pubDate>
      <link>https://forem.com/umairian/from-html-to-pixels-the-magic-behind-web-rendering-48d8</link>
      <guid>https://forem.com/umairian/from-html-to-pixels-the-magic-behind-web-rendering-48d8</guid>
      <description>&lt;p&gt;When you load a website, your browser works like a sophisticated artist, transforming the raw code (HTML, CSS, JavaScript) behind the scenes into the polished picture you see on screen. This journey involves several critical steps: DOM, CSSOM, render tree, layout, paint, and composite. Understanding how these work and where they can bottleneck performance can help you write better, more efficient code. It can also give you an edge in technical interviews. Let’s break it down.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;The DOM: Structure from HTML&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The first step in rendering a website is parsing the HTML to build the &lt;strong&gt;Document Object Model (DOM)&lt;/strong&gt;. Think of the DOM as a tree where each HTML tag becomes a node. The &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt;, and other elements are nested according to their hierarchy.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it does:&lt;/strong&gt; The DOM represents the structure of the document and acts as a bridge between the HTML code and JavaScript. As the browser parses the HTML, it incrementally builds the DOM tree. We need the DOM because it allows JavaScript to interact with and manipulate the page's content dynamically, enabling responsive and interactive web experiences.
The HTML parser in the rendering engine is responsible for building the DOM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caveat:&lt;/strong&gt; Any errors or issues in HTML (like missing closing tags) might break the structure or cause unexpected behaviors. Tools like the browser's Developer Tools help catch such mistakes early.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h4&gt;
  
  
  2. &lt;strong&gt;The CSSOM: Style Rules from CSS&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;While the DOM provides the structure, the &lt;strong&gt;CSS Object Model (CSSOM)&lt;/strong&gt; provides the style. As your CSS files are parsed, the browser builds another tree-like structure that maps styles to different elements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it does:&lt;/strong&gt; The CSSOM is responsible for applying the styles you’ve written—colors, margins, fonts, etc.
The CSS parser, also part of the rendering engine, builds the CSSOM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caveat:&lt;/strong&gt; CSS files can block rendering. The browser waits until the CSSOM is built before rendering anything on the screen, which is why proper CSS loading (using &lt;code&gt;async&lt;/code&gt; or &lt;code&gt;defer&lt;/code&gt;) can significantly speed up page load times.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;h4&gt;
  
  
  3. &lt;strong&gt;The Render Tree: Merging DOM and CSSOM&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Once the DOM and CSSOM are created, the browser merges these two trees into the &lt;strong&gt;render tree&lt;/strong&gt;. This tree contains only the visible elements and the styles applied to them. Hidden elements (e.g., &lt;code&gt;display: none&lt;/code&gt;) are ignored because they don’t affect the visual output.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it does:&lt;/strong&gt; The render tree determines which elements need to be displayed and how they should look. It excludes non-visual elements like &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; and any element with &lt;code&gt;display: none&lt;/code&gt;.
The rendering engine combines the DOM and CSSOM to generate the render tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caveat:&lt;/strong&gt; &lt;code&gt;visibility: hidden&lt;/code&gt; elements still appear in the render tree, which can lead to unnecessary calculations. Be cautious with hidden elements—they can still impact performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The browser transforms HTML and CSS into trees (DOM, CSSOM, and the render tree) because working directly with raw HTML and CSS is inefficient, unmanageable and deoptimized.&lt;/p&gt;

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

&lt;h4&gt;
  
  
  4. &lt;strong&gt;Layout: Calculating Position and Size&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;layout&lt;/strong&gt; step is where the browser calculates the exact position and size of each element in the render tree. This is also known as "reflow." The browser needs to determine the width, height, and spatial positioning of all the elements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it does:&lt;/strong&gt; Layout is responsible for ensuring elements are positioned according to the flow of the document or the CSS properties like &lt;code&gt;float&lt;/code&gt; or &lt;code&gt;absolute&lt;/code&gt;.
The layout engine or reflow engine, which is part of the rendering engine, calculates the positions and sizes of elements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caveat:&lt;/strong&gt; Frequent layout changes (e.g., modifying the DOM or CSS dynamically) can cause a performance hit due to reflows. Recalculating layout can be expensive, especially for complex pages.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  5. &lt;strong&gt;Paint: Filling in Pixels&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In the &lt;strong&gt;paint&lt;/strong&gt; phase, the browser fills in the pixels for each element. The content is drawn on the screen based on the styles and layout computed earlier: fonts, colors, shadows, borders, backgrounds, and more.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it does:&lt;/strong&gt; The browser paints each element pixel-by-pixel onto the screen.
The paint module in the rendering engine takes charge of this phase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caveat:&lt;/strong&gt; Complex visual effects like box shadows, gradients, and large images can slow down this process. Optimizing images and avoiding overly complex styles ensures smooth painting.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  6. &lt;strong&gt;Composite: Putting It All Together&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;composite&lt;/strong&gt; stage involves taking the different painted layers (sometimes, complex web pages are broken into multiple layers) and putting them together to create the final image. Modern browsers use layers to allow for smoother animations and interactions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it does:&lt;/strong&gt; Layers are merged to form the final, visible webpage.
The compositor thread handles the compositing process, ensuring layers are merged and displayed smoothly on the screen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caveat:&lt;/strong&gt; Incorrect layer management can result in janky animations or slow scrolling. Avoid unnecessary layers, but use them for performance-intensive animations (like &lt;code&gt;transform&lt;/code&gt; and &lt;code&gt;opacity&lt;/code&gt; changes) to keep repaints and reflows at a minimum.&lt;/li&gt;
&lt;/ul&gt;

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

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>html</category>
    </item>
    <item>
      <title>Mastering Script Tags: Using Async and Defer for Precise Script Control</title>
      <dc:creator>Umair Syed</dc:creator>
      <pubDate>Sun, 29 Sep 2024 11:05:48 +0000</pubDate>
      <link>https://forem.com/umairian/mastering-script-tags-using-async-and-defer-for-precise-script-control-d9n</link>
      <guid>https://forem.com/umairian/mastering-script-tags-using-async-and-defer-for-precise-script-control-d9n</guid>
      <description>&lt;p&gt;In the world of web development, optimizing page load times is crucial. Two powerful attributes of the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag – &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; – can significantly impact your website's performance. Using these attributes without thoroughly understanding them can affect performance and lead to bugs. Let's start from the basics and learn what these attributes do and when to use them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Basics: How Scripts Load
&lt;/h2&gt;

&lt;p&gt;By default, when a browser encounters a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag, it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pauses HTML parsing&lt;/li&gt;
&lt;li&gt;Downloads the script&lt;/li&gt;
&lt;li&gt;Executes the script&lt;/li&gt;
&lt;li&gt;Resumes HTML parsing&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This process can slow down page rendering, especially for large scripts or slow connections. Additionally, it can lead to bugs if the script runs before certain HTML elements are fully loaded, which often happens if the script is not placed correctly in the document.&lt;/p&gt;

&lt;h2&gt;
  
  
  Async and Defer: A Double-Edged Sword
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Async
&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;script &lt;/span&gt;&lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"script.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it does:&lt;/strong&gt; Downloads the script asynchronously while HTML parsing continues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When it executes:&lt;/strong&gt; As soon as it's downloaded, pausing HTML parsing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When it is used:&lt;/strong&gt; Independent scripts that don't rely on other scripts or DOM content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caveat:&lt;/strong&gt; Can execute out of order, potentially breaking dependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Defer
&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;script &lt;/span&gt;&lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"script.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What it does:&lt;/strong&gt; Downloads the script while HTML parsing continues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When it executes:&lt;/strong&gt; After HTML parsing is complete, but before the &lt;code&gt;DOMContentLoaded&lt;/code&gt; event.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When it is used:&lt;/strong&gt; Scripts that rely on DOM content or need to execute in a specific order.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caveat:&lt;/strong&gt; May delay execution of critical functionality.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Comparing Behaviors
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Attribute&lt;/th&gt;
&lt;th&gt;Download&lt;/th&gt;
&lt;th&gt;Execution&lt;/th&gt;
&lt;th&gt;HTML Parsing&lt;/th&gt;
&lt;th&gt;Main Risk&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;Blocks&lt;/td&gt;
&lt;td&gt;Immediate&lt;/td&gt;
&lt;td&gt;Paused&lt;/td&gt;
&lt;td&gt;Slow Initial Render&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Async&lt;/td&gt;
&lt;td&gt;Parallel&lt;/td&gt;
&lt;td&gt;ASAP&lt;/td&gt;
&lt;td&gt;Paused when downloaded&lt;/td&gt;
&lt;td&gt;Race Conditions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Defer&lt;/td&gt;
&lt;td&gt;Parallel&lt;/td&gt;
&lt;td&gt;After HTML&lt;/td&gt;
&lt;td&gt;Continues&lt;/td&gt;
&lt;td&gt;Delayed Functionality&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Order of Execution: Async, Defer, and Both
&lt;/h2&gt;

&lt;p&gt;Understanding the order of execution for scripts with different attributes is crucial for managing dependencies and ensuring proper functionality. Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Regular scripts (no async or defer):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Execute in the order they appear in the document.&lt;/li&gt;
&lt;li&gt;Block HTML parsing until they're downloaded and executed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Async scripts:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download in parallel and execute as soon as they're available.&lt;/li&gt;
&lt;li&gt;No guaranteed order of execution; they run as soon as they're downloaded.&lt;/li&gt;
&lt;li&gt;May execute before the DOM is fully loaded.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Defer scripts:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Download in parallel but execute only after HTML parsing is complete.&lt;/li&gt;
&lt;li&gt;Execute in the order they appear in the document.&lt;/li&gt;
&lt;li&gt;Run before the &lt;code&gt;DOMContentLoaded&lt;/code&gt; event.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Scripts with both async and defer:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;async&lt;/code&gt; attribute takes precedence in modern browsers.&lt;/li&gt;
&lt;li&gt;In older browsers that don't support &lt;code&gt;async&lt;/code&gt;, they fall back to &lt;code&gt;defer&lt;/code&gt; behavior.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example Execution Order:
&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;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"1.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"2.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"3.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"4.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;defer&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"5.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Possible execution order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;1.js&lt;/code&gt; (blocks parsing)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;3.js&lt;/code&gt; or &lt;code&gt;2.js&lt;/code&gt; (whichever downloads first)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2.js&lt;/code&gt; or &lt;code&gt;3.js&lt;/code&gt; (whichever downloads second)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;4.js&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;5.js&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note that &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt; could execute in any order or even before &lt;code&gt;1&lt;/code&gt; if &lt;code&gt;1.js&lt;/code&gt; takes longer to download.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Use &lt;code&gt;async&lt;/code&gt; for independent scripts like analytics.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;defer&lt;/code&gt; for scripts that depend on DOM or other scripts.&lt;/li&gt;
&lt;li&gt;Place scripts in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; with &lt;code&gt;async&lt;/code&gt; or &lt;code&gt;defer&lt;/code&gt; to start downloading early.&lt;/li&gt;
&lt;li&gt;For critical scripts, consider inline scripts in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Browser Support
&lt;/h2&gt;

&lt;p&gt;Both &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt; are widely supported in modern browsers. For older browsers, consider using a script loader or placing scripts at the end of the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>html</category>
      <category>frontend</category>
    </item>
    <item>
      <title>The Right Way to Update Reference Types in React State</title>
      <dc:creator>Umair Syed</dc:creator>
      <pubDate>Tue, 10 Sep 2024 17:37:49 +0000</pubDate>
      <link>https://forem.com/umairian/the-right-way-to-update-reference-types-in-react-state-3a3e</link>
      <guid>https://forem.com/umairian/the-right-way-to-update-reference-types-in-react-state-3a3e</guid>
      <description>&lt;p&gt;Ever wondered why your set state function is not updating the state sometimes when the stored value is a reference type like array or object. Let me tell you why. But first, let's build a foundation so that we can cop up with the concepts that will be explained in this blog.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Principle of Immutability in React:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The principle of immutability in React refers to the practice of not directly mutating state or data structures, but instead creating new copies of them whenever changes are needed. In simpler terms, once a value is created, it should not be changed, but rather replaced with a new value when modifications are required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reference Types:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All the data types in JavaScript are divided into two main categories:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Primitive Types&lt;/li&gt;
&lt;li&gt;Non primitive or Reference Types&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Primitive types are the basic building blocks or fundamental types of data. For example, numbers, strings, Boolean, null and undefined etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Primitive types&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Umair Syed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// String&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// number&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;isCool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// boolean&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;favoriteMovie&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// null&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reference types are complex types that are made up from other types like Primitive or even other reference types. You can consider them as a combination of multiple primitive types in a single place. For example, an object or array where we can store a group of values including strings, numbers, Booleans and so on. In JavaScript, reference types are Arrays, Objects, and Functions.&lt;br&gt;
Why are they called reference types? Because in JavaScript, when you create a variable and assign it a reference type value, you're not actually storing the entire value in that variable. Instead, you're storing a reference to where the value is stored in memory, compared to Primitive type where value is stored in that variable. This means that if you have two variables pointing to the same reference type value, they both refer to the same data in memory. As a result, changes made to the data through one variable will also be reflected when accessing the data through the other variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Reference types&lt;/span&gt;

&lt;span class="c1"&gt;// These values are stored in a heap and person variable is just storing &lt;/span&gt;
&lt;span class="c1"&gt;// the pointer to that place in memory. You can think of it as&lt;/span&gt;
&lt;span class="c1"&gt;// person = 0x7ffd84520a38&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Umair Syed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Since we assigned an object to anotherPerson, that is why a pointer is &lt;/span&gt;
&lt;span class="c1"&gt;// stored in anotherPerson as well. A new pointer? Nope, the same&lt;/span&gt;
&lt;span class="c1"&gt;// pointer as stored in person. For the sake of example:&lt;/span&gt;
&lt;span class="c1"&gt;// anotherPerson = 0x7ffd84520a38;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;anotherPerson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Since both objects have the same pointer, changing any property in&lt;/span&gt;
&lt;span class="c1"&gt;// anotherPerson will be reflected in person as well&lt;/span&gt;
&lt;span class="nx"&gt;anotherPerson&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Faisal Tahir&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: "Faisal Tahir", age: 25 }&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;anotherPerson&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// { name: "Faisal Tahir", age: 25 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keeping the above points in mind, it is very crucial to take care of the type that we are updating in the React state. Now, what is the wrong way of updating a state that has reference value and what is the correct way?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wrong Way:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's say you are showing an array of sorted values in a list, and you want to change the sorting order from Ascending to Descending or vice versa. How you would do it. Most probably, the novice will think of the following way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;OrderedList&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;sortedArr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setSortedArr&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ol&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;sortedArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;))}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ol&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;
        &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setSortedArr&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;())}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Reverse&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On clicking the Reverse button, the list will not update, why? Let's understand:&lt;br&gt;
The reverse() method reverses the elements of an array in place, meaning it modifies the original array and returns a reference to the same array with its elements reversed.&lt;br&gt;
Since reverse() modifies the original array, React might not detect the change in the array's content. React compares the new state with the previous state using reference equality and shallow comparison. If the reference to the array remains the same after the update (which is the case when using reverse()), React might not recognize the change and may not trigger a re-render.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Correct Way:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To fix this issue, you should ensure that you create a new array with the reversed elements rather than modifying the original array in place. You can achieve this using the spread operator (...) or other array methods like slice() or concat():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setSortedArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentValue&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way, you're creating a new array with the reversed elements, maintaining immutability, and ensuring that React detects the change in state correctly, triggering a re-render as expected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt; It's essential to always create a new array or object when updating state that contains a reference type. This ensures adherence to the principle of immutability, which is crucial for React's state management to function correctly. By creating new copies of arrays or objects instead of modifying them directly, you guarantee that React accurately detects changes and triggers re-renders as needed, maintaining the integrity of your application's state.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
