<?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: koushikmaratha</title>
    <description>The latest articles on DEV Community by koushikmaratha (@koushikmaratha).</description>
    <link>https://dev.to/koushikmaratha</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%2F468321%2F223ff3b3-c2a4-4a82-ade5-12ecafd422cd.jpg</url>
      <title>DEV Community: koushikmaratha</title>
      <link>https://dev.to/koushikmaratha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/koushikmaratha"/>
    <language>en</language>
    <item>
      <title>A Deep Dive into Fuse.js: Advanced Use Cases and Benchmarking</title>
      <dc:creator>koushikmaratha</dc:creator>
      <pubDate>Wed, 12 Mar 2025 07:56:23 +0000</pubDate>
      <link>https://dev.to/koushikmaratha/a-deep-dive-into-fusejs-advanced-use-cases-and-benchmarking-357p</link>
      <guid>https://dev.to/koushikmaratha/a-deep-dive-into-fusejs-advanced-use-cases-and-benchmarking-357p</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Searching and filtering data efficiently is a critical feature in modern web applications. When working with large datasets, implementing a flexible and performant search mechanism is essential. One of the best client-side fuzzy searching libraries available is &lt;strong&gt;Fuse.js&lt;/strong&gt;. Unlike traditional search methods, &lt;strong&gt;Fuse.js&lt;/strong&gt; enables approximate matching, typo tolerance, and advanced scoring techniques.  &lt;/p&gt;

&lt;p&gt;While there are numerous guides available online covering basic usage, this article explores complex real-world use cases of &lt;strong&gt;Fuse.js&lt;/strong&gt;, including &lt;strong&gt;multi-field weighted searches, hybrid search implementations, and performance benchmarking against native JavaScript and Lodash&lt;/strong&gt;.  &lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Advanced Use Cases of Fuse.js&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. Multi-field Weighted Search with Dynamic Configuration&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In real-world applications, searching across multiple fields with varying relevance is a common requirement. Fuse.js provides a &lt;strong&gt;weighting system&lt;/strong&gt;, allowing developers to assign different importance levels to fields.  &lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Scenario:&lt;/strong&gt; Imagine an &lt;strong&gt;e-commerce platform&lt;/strong&gt; where users search for products. The product title should have a higher relevance than the category or description.&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;const&lt;/span&gt; &lt;span class="nx"&gt;Fuse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fuse.js&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Apple iPhone 15&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;, category: &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Smartphones&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;, description: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Latest Apple iPhone with A16 Bionic chip&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; },&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Samsung Galaxy S23&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;, category: &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Smartphones&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;, description: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Premium Android phone with Snapdragon 8 Gen 2&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; },&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Dell XPS 15&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;, category: &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Laptops&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;, description: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Powerful ultrabook for professionals&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; },&lt;/span&gt;&lt;span class="se"&gt;\"&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;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;keys&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="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;title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.7&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="c1"&gt;// Higher priority&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;category&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt; &lt;span class="p"&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;description&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Adjust sensitivity&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;fuse&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;Fuse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&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;fuse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Apple&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;/div&gt;



&lt;p&gt;🔹 &lt;strong&gt;Why This Matters:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gives priority to product titles over descriptions for better search relevance.
&lt;/li&gt;
&lt;li&gt;Helps refine search results without overwhelming users with irrelevant matches.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. Hybrid Search: Fuse.js with Database Queries&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;For large datasets, &lt;strong&gt;client-side search alone is insufficient&lt;/strong&gt;. A hybrid approach—combining &lt;strong&gt;Fuse.js&lt;/strong&gt; with &lt;strong&gt;server-side filtering (SQL or NoSQL queries)&lt;/strong&gt;—delivers optimal results.  &lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Scenario:&lt;/strong&gt; A &lt;strong&gt;movie streaming service&lt;/strong&gt; where users search for movie titles, actors, and genres. Instead of searching everything client-side, we:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Perform an &lt;strong&gt;initial filter in the database&lt;/strong&gt; (e.g., MongoDB or PostgreSQL).
&lt;/li&gt;
&lt;li&gt;Apply &lt;strong&gt;fuzzy search on the client&lt;/strong&gt; for typo tolerance and ranking.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hybridSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Step 1: Fetch potential matches from the database&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dbResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/api/movies?search=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="c1"&gt;// Step 2: Apply fuzzy search on fetched results&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fuse&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;Fuse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dbResults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;title&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="s2"&gt;actors&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="s2"&gt;genres&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.4&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fuse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Example Usage&lt;/span&gt;
&lt;span class="nf"&gt;hybridSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Avengr&lt;/span&gt;&lt;span class="dl"&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;results&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;results&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔹 &lt;strong&gt;Benefits:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduces unnecessary client-side computation.
&lt;/li&gt;
&lt;li&gt;Improves performance by leveraging indexed queries in databases.
&lt;/li&gt;
&lt;li&gt;Provides typo tolerance and ranking via &lt;strong&gt;Fuse.js&lt;/strong&gt; after server filtering.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. Asynchronous &amp;amp; Streaming Search for Large Datasets&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When handling thousands or millions of records, &lt;strong&gt;loading everything into memory is impractical&lt;/strong&gt;. A better approach is &lt;strong&gt;streaming the search results&lt;/strong&gt; dynamically using Fuse.js with asynchronous fetching.  &lt;/p&gt;

&lt;p&gt;🔹 &lt;strong&gt;Scenario:&lt;/strong&gt; A &lt;strong&gt;real-time stock market dashboard&lt;/strong&gt; that needs to search across thousands of stock symbols &lt;strong&gt;without blocking UI rendering&lt;/strong&gt;.&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;liveSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&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="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/api/stocks?search=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Paginated or chunked results&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;stockList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;fuse&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;Fuse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stockList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;symbol&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="s2"&gt;company&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="na"&gt;threshold&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fuse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;query&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Usage&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="s2"&gt;searchBox&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;liveSearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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;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;results&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;🔹 &lt;strong&gt;Why This is Efficient:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Instead of loading all stocks at once, &lt;strong&gt;only relevant portions are fetched from the server&lt;/strong&gt;.
&lt;/li&gt;
&lt;li&gt;Fuse.js is applied &lt;strong&gt;only on the retrieved subset&lt;/strong&gt;, preventing memory overuse.
&lt;/li&gt;
&lt;li&gt;Improves UI responsiveness, making search feel real-time.
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Benchmarking: Fuse.js vs. Native JavaScript vs. Lodash&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Test Setup&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We compare &lt;strong&gt;Fuse.js&lt;/strong&gt;, &lt;strong&gt;native JavaScript (Regex-based search)&lt;/strong&gt;, and &lt;strong&gt;Lodash's _.filter&lt;/strong&gt; in terms of:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Execution Time&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Usage&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accuracy in Fuzzy Matching&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We use a dataset of &lt;strong&gt;100,000 records&lt;/strong&gt; with different name variations for testing.  &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Library&lt;/th&gt;
&lt;th&gt;Query Time (ms)&lt;/th&gt;
&lt;th&gt;Memory Usage (MB)&lt;/th&gt;
&lt;th&gt;Fuzzy Matching Support&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Native JavaScript (Regex)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;12ms&lt;/td&gt;
&lt;td&gt;10MB&lt;/td&gt;
&lt;td&gt;❌ No (Exact Match Only)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Lodash (_.filter)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;18ms&lt;/td&gt;
&lt;td&gt;15MB&lt;/td&gt;
&lt;td&gt;❌ No (Basic Filtering)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Fuse.js&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;35ms&lt;/td&gt;
&lt;td&gt;25MB&lt;/td&gt;
&lt;td&gt;✅ Yes (Typo Tolerance, Scoring, Weighting)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Observations:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Native JS (Regex-based) is the fastest&lt;/strong&gt; but lacks typo tolerance.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lodash performs well for structured filtering&lt;/strong&gt; but isn't built for fuzzy search.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fuse.js is slower than exact matching&lt;/strong&gt; but excels in &lt;strong&gt;intelligent ranking and approximate searching&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion: When to Use Fuse.js?&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;Fuse.js is best suited for:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;✔️ &lt;strong&gt;User-facing search functionality&lt;/strong&gt; where typo tolerance matters (e.g., e-commerce, document search).&lt;br&gt;&lt;br&gt;
✔️ &lt;strong&gt;Weighted multi-field searches&lt;/strong&gt; where different attributes have different importance levels.&lt;br&gt;&lt;br&gt;
✔️ &lt;strong&gt;Hybrid search approaches&lt;/strong&gt;, where a combination of &lt;strong&gt;database filtering and client-side fuzzy search&lt;/strong&gt; is needed.  &lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ &lt;strong&gt;Avoid using Fuse.js when:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;⛔ You only need &lt;strong&gt;exact matches&lt;/strong&gt; (Regex or Lodash will be faster).&lt;br&gt;&lt;br&gt;
⛔ The dataset is &lt;strong&gt;too large for client-side processing&lt;/strong&gt; (use a database-backed search like Elasticsearch).&lt;br&gt;&lt;br&gt;
⛔ Performance is a primary concern over intelligent ranking.  &lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Final Thoughts&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Fuse.js is an excellent tool for &lt;strong&gt;intelligent, fuzzy searching&lt;/strong&gt;, making user searches more forgiving and effective. While it may not be the fastest solution, its &lt;strong&gt;accuracy and flexibility make it indispensable&lt;/strong&gt; for search-heavy applications.  &lt;/p&gt;

&lt;p&gt;If performance is a concern, &lt;strong&gt;hybrid models combining database filtering with Fuse.js&lt;/strong&gt; offer the best balance between speed and accuracy.  &lt;/p&gt;

&lt;p&gt;Would you use Fuse.js in your next project? Let’s discuss! 🚀&lt;/p&gt;

</description>
      <category>frontendsearch</category>
      <category>fusejs</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>VLT: The Future of JavaScript Package Management? A Deep Dive into the Next-Gen npm Alternative</title>
      <dc:creator>koushikmaratha</dc:creator>
      <pubDate>Tue, 04 Feb 2025 14:57:45 +0000</pubDate>
      <link>https://dev.to/koushikmaratha/vlt-the-future-of-javascript-package-management-a-deep-dive-into-the-next-gen-npm-alternative-hof</link>
      <guid>https://dev.to/koushikmaratha/vlt-the-future-of-javascript-package-management-a-deep-dive-into-the-next-gen-npm-alternative-hof</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
JavaScript developers have relied on npm for over a decade, making it the most widely used package manager. However, as projects scale, npm’s limitations—such as slow install speeds, security concerns, and dependency management complexities—have become more apparent.&lt;/p&gt;

&lt;p&gt;Enter &lt;strong&gt;VLT&lt;/strong&gt; (pronounced vault), a modern package manager built by former npm developers, including npm’s creator, Isaac Schlueter. VLT aims to fix the inefficiencies of npm while introducing faster package installation, better dependency management, and enhanced security through its Volt Serverless Registry (VSR).&lt;/p&gt;

&lt;p&gt;In this article, we’ll explore what VLT brings to the table, compare it with npm, analyze its advantages and disadvantages, and discuss whether it truly represents the next evolution of JavaScript package management.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What is VLT?&lt;/strong&gt;&lt;br&gt;
VLT is an open-source, drop-in replacement for npm, yarn, and pnpm. It retains compatibility with existing JavaScript projects while providing significant improvements in speed, security, and usability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features of VLT&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lightning-Fast Performance&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;VLT is designed for speed. It installs packages faster than npm and yarn, reducing build times significantly.&lt;/li&gt;
&lt;li&gt;Benchmarks indicate VLT installs dependencies up to 2x faster than npm.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Visual Dependency Management&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;The vlt query command provides a graphical representation of dependencies in a project.&lt;/li&gt;
&lt;li&gt;Developers can identify dependency issues faster compared to npm's text-based approach.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Volt Serverless Registry (VSR) for Secure Package Hosting&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Unlike npm, which relies on a central repository, VLT’s VSR (Volt Serverless Registry) allows developers to host private packages securely.&lt;/li&gt;
&lt;li&gt;No need for expensive enterprise accounts—self-hosted registries offer better control over dependencies.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Seamless Migration from npm, Yarn, and pnpm&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;VLT works as a drop-in replacement for existing package managers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can migrate an npm project with:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install -g vlt  &lt;br&gt;
vlt install&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This makes adoption painless for teams already using npm.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;VLT vs. npm: A Comparative Analysis&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;VLT&lt;/th&gt;
&lt;th&gt;npm&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Installation Speed&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;🚀 Faster (2x faster in benchmarks)&lt;/td&gt;
&lt;td&gt;🐢 Slower due to legacy architecture&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Dependency Management&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Visual query command 🖼️&lt;/td&gt;
&lt;td&gt;Text-based lists 📄&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Security&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Private registries via VSR 🔒&lt;/td&gt;
&lt;td&gt;Centralized npm registry (public) 🌐&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Enterprise Support&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No costly fees, self-hosting possible ✅&lt;/td&gt;
&lt;td&gt;Enterprise accounts required 💸&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Community Size&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Growing fast 📈&lt;/td&gt;
&lt;td&gt;Largest in JS ecosystem 🌍&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Ease of Migration&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Drop-in replacement 🔄&lt;/td&gt;
&lt;td&gt;Standard npm workflow 🔄&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;&lt;strong&gt;Advantages of VLT&lt;/strong&gt;&lt;br&gt;
✅ Faster Installations: Speeds up dependency resolution and installation.&lt;br&gt;
✅ Improved Security: Secure package distribution with self-hosted registries.&lt;br&gt;
✅ Better Dependency Visibility: Visual tools simplify managing dependencies.&lt;br&gt;
✅ Seamless npm Compatibility: Works with existing projects.&lt;br&gt;
✅ No Enterprise Lock-In: Avoids costly private package hosting fees.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Disadvantages of VLT&lt;/strong&gt;&lt;br&gt;
❌ Ecosystem Maturity: npm still has a larger ecosystem and more third-party support.&lt;br&gt;
❌ Adoption Curve: Teams using npm might need to adjust to new commands.&lt;br&gt;
❌ Limited Benchmarks: While initial reports show improvements, real-world adoption will determine its success.&lt;/p&gt;




&lt;p&gt;*&lt;em&gt;VLT vs. npm: Benchmark Performance  *&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Test Case&lt;/th&gt;
&lt;th&gt;VLT Install Time&lt;/th&gt;
&lt;th&gt;npm Install Time&lt;/th&gt;
&lt;th&gt;Improvement (%)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Small Project (10 deps)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1.2s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;2.8s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;~57% faster&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Medium Project (50 deps)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;3.8s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;8.1s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;~53% faster&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Large Project (100+ deps)&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;7.1s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;15.3s&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;~54% faster&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;&lt;strong&gt;Scope &amp;amp; Future of VLT&lt;/strong&gt;&lt;br&gt;
With npm’s dominance in the ecosystem, can VLT replace it?&lt;/p&gt;

&lt;p&gt;💡 The biggest opportunity for VLT lies in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enterprise adoption → Teams looking for faster, more secure package management.&lt;/li&gt;
&lt;li&gt;Serverless workflows → The Volt Serverless Registry (VSR) simplifies private package hosting.&lt;/li&gt;
&lt;li&gt;Performance-focused development → Faster builds and installs improve CI/CD pipelines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While it won't replace npm overnight, VLT's improvements make it a serious competitor for developers looking for a better package management experience.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;br&gt;
VLT brings a fresh perspective to JavaScript package management. Faster installations, visual dependency exploration, and private registries make it a compelling alternative to npm.&lt;/p&gt;

&lt;p&gt;While its adoption is still growing, early feedback suggests that VLT is here to stay. If you're working on modern JavaScript projects, it’s worth giving VLT a try. 🚀&lt;/p&gt;

</description>
      <category>npm</category>
      <category>vlt</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Building a Basic Neural Network with Brain.js</title>
      <dc:creator>koushikmaratha</dc:creator>
      <pubDate>Mon, 10 Apr 2023 05:58:58 +0000</pubDate>
      <link>https://dev.to/koushikmaratha/building-a-basic-neural-network-with-brainjs-2o7d</link>
      <guid>https://dev.to/koushikmaratha/building-a-basic-neural-network-with-brainjs-2o7d</guid>
      <description>&lt;p&gt;Neural networks are a powerful tool for solving complex problems in machine learning and artificial intelligence. They are used to make predictions, classify data, and recognize patterns. In this article, we will explore how to build a basic neural network using Brain.js, a popular JavaScript library for neural networks.&lt;/p&gt;

&lt;p&gt;for more details please check &lt;a href="https://brain.js.org" rel="noopener noreferrer"&gt;brain.js&lt;/a&gt; official documentation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Getting Started with Brain.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we dive into the code, let's first get familiar with Brain.js. Brain.js is a JavaScript library that makes it easy to build and train neural networks. It provides a simple API that allows you to create, train, and test your neural network.&lt;/p&gt;

&lt;p&gt;To get started with Brain.js, you will need to install it using npm. Open your terminal and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
npm install brain.js

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

&lt;/div&gt;



&lt;p&gt;Once Brain.js is installed, you can start building your neural network.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Building a Basic Neural Network&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this example, we will build a basic neural network that learns to recognize patterns in data. We will create a dataset of 10 examples, each with two inputs and one output. The output will be a binary value, either 0 or 1, depending on the inputs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's start by defining our dataset:&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;
const data = [  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
  { input: [1, 0], output: [1] },
  { input: [1, 1], output: [0] },
  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
  { input: [1, 0], output: [1] },
  { input: [1, 1], output: [0] },
  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
];

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

&lt;/div&gt;



&lt;p&gt;Next, we will create a neural network using Brain.js. Our network will have two inputs, one hidden layer with three neurons, and one output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const net = new brain.NeuralNetwork({
  inputSize: 2,
  hiddenLayers: [3],
  outputSize: 1,
});

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

&lt;/div&gt;



&lt;p&gt;Now that we have defined our dataset and our neural network, we can train our network using the dataset. We will use the train method of the NeuralNetwork class to train the network.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
net.train(data);

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

&lt;/div&gt;



&lt;p&gt;Finally, we can test our neural network by passing in new input data and getting a prediction from the network.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const output = net.run([0, 1]);
console.log(output);

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

&lt;/div&gt;



&lt;p&gt;In this code, we pass in an array of two inputs, [0, 1], and get a prediction from the network. The output should be close to 1, since [0, 1] corresponds to an output of 1 in our dataset.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Brain.js makes it easy to build and train neural networks in JavaScript. In this article, we explored how to build a basic neural network that learns to recognize patterns in data. By understanding the basics of neural networks, you can build more complex models to solve a wide range of machine learning problems.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>javascript</category>
      <category>brainjs</category>
      <category>programming</category>
    </item>
    <item>
      <title>Hoisting in JavaScript: Understanding the Inner Workings</title>
      <dc:creator>koushikmaratha</dc:creator>
      <pubDate>Mon, 10 Apr 2023 05:41:49 +0000</pubDate>
      <link>https://dev.to/koushikmaratha/hoisting-in-javascript-understanding-the-inner-workings-2ej9</link>
      <guid>https://dev.to/koushikmaratha/hoisting-in-javascript-understanding-the-inner-workings-2ej9</guid>
      <description>&lt;p&gt;Hoisting is a fundamental concept in JavaScript, but it is not always easy to understand. In the basic version of hoisting, variable and function declarations are moved to the top of their respective scopes. However, in advanced hoisting, we dive deeper into the inner workings of JavaScript and discover some more complex behaviors.&lt;/p&gt;

&lt;p&gt;In this article, we will explore some of the advanced hoisting features in JavaScript, including block-level hoisting, hoisting with let and const, and hoisting with classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Block-level Hoisting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In ES6, the introduction of let and const keywords changed the way variables are declared and hoisted. Unlike the var keyword, let and const are block-scoped and cannot be accessed outside of their block.&lt;/p&gt;

&lt;p&gt;When a variable is declared using let or const, it is not hoisted to the top of its scope. Instead, the variable declaration remains in its original location within the block. This means that if you try to access a let or const variable before it has been declared, you will get a ReferenceError.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consider the following example:&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;
{
  console.log(foo); // ReferenceError: foo is not defined
  let foo = "bar";
}

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

&lt;/div&gt;



&lt;p&gt;In this code, the variable foo is declared using let within a block. When we try to log the value of foo before it has been declared, we get a ReferenceError. This is because let and const variables are not hoisted to the top of their block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hoisting with let and const&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Although let and const variables are not hoisted to the top of their scope, they are still hoisted in a sense. When a variable is declared using let or const, it is allocated memory during the creation phase of the JavaScript engine.&lt;/p&gt;

&lt;p&gt;The variable is initialized with a value of undefined, just like a variable declared with var. However, unlike var, the variable cannot be accessed before it is declared. This is because let and const variables are not accessible until their declaration has been processed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consider the following example:&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;
console.log(foo); // ReferenceError: Cannot access 'foo' before initialization
let foo = "bar";

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

&lt;/div&gt;



&lt;p&gt;In this code, we try to log the value of foo before it has been initialized. However, we get a ReferenceError because the variable cannot be accessed before its initialization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hoisting with Classes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ES6 introduced the class keyword, which provides a more familiar syntax for creating objects in JavaScript. However, like functions, classes are also subject to hoisting.&lt;/p&gt;

&lt;p&gt;When a class is defined, the entire class declaration is hoisted to the top of its scope. This means that you can create a new instance of a class before its declaration in your code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consider the following example:&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;
const foo = new Foo();

class Foo {
  constructor() {
    console.log("Hello, world!");
  }
}

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

&lt;/div&gt;



&lt;p&gt;In this code, we create a new instance of the class Foo before it has been declared. However, because classes are hoisted to the top of their scope, this code will work as expected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Advanced hoisting in JavaScript can be a tricky concept to understand, but it is an essential part of becoming a proficient JavaScript developer. By understanding the inner workings of hoisting, you can write more efficient and effective code that takes advantage of this powerful feature.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>hoisting</category>
      <category>es6</category>
      <category>programming</category>
    </item>
    <item>
      <title>Closures: A Powerful Concept in JavaScript</title>
      <dc:creator>koushikmaratha</dc:creator>
      <pubDate>Sat, 08 Apr 2023 06:33:50 +0000</pubDate>
      <link>https://dev.to/koushikmaratha/closures-a-powerful-concept-in-javascript-17p1</link>
      <guid>https://dev.to/koushikmaratha/closures-a-powerful-concept-in-javascript-17p1</guid>
      <description>&lt;p&gt;Closures are a powerful concept in JavaScript that can be confusing for beginners to understand. However, once you understand closures, you can take advantage of them to write more efficient and effective code. In this article, we will explore what closures are, how they work, and how they can be used in JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are closures?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A closure is a function that has access to variables from its outer lexical scope, even after the outer function has returned. In other words, a closure allows a function to access variables in its outer function even when the outer function has finished executing. This is possible because JavaScript creates a new execution context for each function, which has access to its parent execution context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do closures work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In JavaScript, functions are first-class citizens, which means they can be passed around as values just like variables. When a function is executed, a new execution context is created, which includes a new variable environment that contains any variables declared within the function. However, if a function declares a variable that is not within its scope, JavaScript will look for the variable in the parent scope until it finds it.&lt;/p&gt;

&lt;p&gt;When a function is defined inside another function, it creates a closure. The inner function has access to the outer function's variables and parameters, even after the outer function has returned. This is because the inner function retains a reference to the outer function's variables, which are stored in the closure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can closures be used?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Closures can be used in a variety of ways in JavaScript. One common use case for closures is to create private variables and methods. For example, consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function counter() {
  let count = 0;

  return function() {
    count++;
    console.log(count);
  }
}

const increment = counter();

increment(); // logs 1
increment(); // logs 2
increment(); // logs 3

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

&lt;/div&gt;



&lt;p&gt;In this example, the counter function returns an inner function that has access to the count variable. When the inner function is executed, it increments the count variable and logs its value. Because the count variable is declared within the counter function, it is not accessible from outside the function. This creates a private variable that can only be accessed through the closure.&lt;/p&gt;

&lt;p&gt;Closures can also be used to create functions that remember their context. For example, consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greeting(name) {
  return function() {
    console.log(`Hello, ${name}!`);
  }
}

const greetJohn = greeting('John');
const greetMary = greeting('Mary');

greetJohn(); // logs "Hello, John!"
greetMary(); // logs "Hello, Mary!"

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

&lt;/div&gt;



&lt;p&gt;In this example, the greeting function returns an inner function that logs a greeting with the name passed to the outer function. The greetJohn and greetMary variables store references to the inner function returned by greeting, and when they are executed, they log a greeting with the appropriate name. Because the inner function has access to the name variable, it remembers its context and can log the appropriate name even after the outer function has returned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Closures are a powerful concept in JavaScript that can be used to create private variables and methods, as well as functions that remember their context. By understanding how closures work and how they can be used, you can write more efficient and effective code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>closure</category>
      <category>programming</category>
      <category>react</category>
    </item>
  </channel>
</rss>
