<?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: Programming In Blood</title>
    <description>The latest articles on DEV Community by Programming In Blood (@programminginblood).</description>
    <link>https://dev.to/programminginblood</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%2F2173308%2Fbc54e1b7-e46a-4131-9c2d-60ae555727bf.png</url>
      <title>DEV Community: Programming In Blood</title>
      <link>https://dev.to/programminginblood</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/programminginblood"/>
    <language>en</language>
    <item>
      <title>Why Set.has() is Faster Than Array.includes() for Finding Items</title>
      <dc:creator>Programming In Blood</dc:creator>
      <pubDate>Thu, 31 Oct 2024 06:19:55 +0000</pubDate>
      <link>https://dev.to/programminginblood/why-sethas-is-faster-than-arrayincludes-for-finding-items-4hh</link>
      <guid>https://dev.to/programminginblood/why-sethas-is-faster-than-arrayincludes-for-finding-items-4hh</guid>
      <description>&lt;p&gt;Sometimes, when building applications, performance ends up being the key or at least a significant driver, especially when dealing with large datasets or real-time requirements. One of the most common tasks in JavaScript is to check whether some value exists in a collection. The two most often used alternatives are &lt;code&gt;Array.includes()&lt;/code&gt; and &lt;code&gt;Set.has()&lt;/code&gt;. Both work, but actually, &lt;code&gt;Set.has()&lt;/code&gt; works better than &lt;code&gt;Array.includes&lt;/code&gt;. Let's dive into the reasons for this and decide when you should use either alternative.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding &lt;code&gt;Array.includes()&lt;/code&gt; vs. &lt;code&gt;Set.has()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;There are two methods that appear to be fairly straightforward in their usage, but have different implementations, namely, &lt;code&gt;Array.includes()&lt;/code&gt; and &lt;code&gt;Set.has()&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Array.includes()
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;includes()&lt;/code&gt; method checks if a given value is present within an array.&lt;br&gt;
It makes use of the time complexity of O(n), such that the greater the length of the array, the longer it will take for the value check.&lt;br&gt;
This is because &lt;code&gt;Array.includes()&lt;/code&gt; searches the array from start to end (or until it finds the value), and the bigger the array, the longer it takes.&lt;/p&gt;

&lt;h4&gt;
  
  
  Set.has()
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;has()&lt;/code&gt; method of a &lt;code&gt;Set&lt;/code&gt; checks if a given value exists as well but does so much quicker.&lt;br&gt;
&lt;code&gt;Set.has()&lt;/code&gt; relies on a hash table-based structure that allows for constant-time lookup, or O(1) time complexity.&lt;br&gt;
Unlike arrays, set is built for handling unique values, so it won't have duplicate values inside of it and has more lookup times.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;Set.has()&lt;/code&gt; is Faster for Large Data Sets
&lt;/h2&gt;

&lt;p&gt;When you use &lt;code&gt;Set.has()&lt;/code&gt;, JavaScript can find the item in one direct operation, no matter how many items are in the set. For example, while checking if a value is in a set that contains a million, the time consumed by &lt;code&gt;Set.has()&lt;/code&gt; will be precisely the same as checking ten.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;code&gt;Array.includes()&lt;/code&gt; examines each element from left to right sequentially until it may either find the item of interest or reaches its very end. That means the longer it is in size, check times take, especially in the case of an item close to the end-and definitely when the item in question is not there. &lt;/p&gt;

&lt;p&gt;Here's a close look at an example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F3xr94xbf9as58hrjtlx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3xr94xbf9as58hrjtlx5.png" alt="comparison.js" width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you run this, you see that &lt;code&gt;Set.has()&lt;/code&gt; far outperforms &lt;code&gt;Array.includes()&lt;/code&gt; on large arrays. In practice, that difference might well translate to smoother animations, faster load times, or even less resource usage on your server.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to Use &lt;code&gt;Set.has()&lt;/code&gt; and &lt;code&gt;Array.includes()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This all depends on what you are trying to achieve. Here is a brief summary:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;Set.has()&lt;/code&gt; if:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You are dealing with big data and are doing lots of lookups.&lt;/li&gt;
&lt;li&gt;You're working with unique values, such as a set of unique user IDs,tags, or keywords.&lt;/li&gt;
&lt;li&gt;You do not mind the small upfront cost of an array conversion into a set to minimize the number of lookups later.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;Array.includes()&lt;/code&gt; if:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your dataset is small and the difference in performance is negligible.&lt;/li&gt;
&lt;li&gt;You need to check for an item only once or a few times, so there is no benefit in creating a Set.&lt;/li&gt;
&lt;li&gt;You are dealing with duplicates, which a set cannot handle.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example Use Case
&lt;/h2&gt;

&lt;p&gt;Imagine you are implementing a user search feature that filters names against a list of blocked words. If you have hundreds of blocked words and are searching frequently, using a Set for the blocked words can make each search check faster:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0u8edtl7a4cki5qa9d74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0u8edtl7a4cki5qa9d74.png" alt="Image description" width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Even in smaller cases, a Set will help keep things efficient and predictable. Plus, if the blocked words list grows, you’ll already have a scalable solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: &lt;code&gt;Set.has()&lt;/code&gt; offers O(1) time complexity, making it much faster than &lt;code&gt;Array.includes()&lt;/code&gt; with O(n) for larger collections&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Suitability&lt;/strong&gt;: &lt;code&gt;Set&lt;/code&gt; is designed for unique values, so it’s naturally optimized for lookups. Arrays are more flexible with duplicate values but are slower at checking for existence.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability&lt;/strong&gt;: As your data grows, &lt;code&gt;Set.has()&lt;/code&gt; continues to perform well, while &lt;code&gt;Array.includes()&lt;/code&gt; will slow down.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;It is always important to know the size and nature of your dataset when choosing between &lt;code&gt;Set.has()&lt;/code&gt; and &lt;code&gt;Array.includes()&lt;/code&gt;. &lt;code&gt;Array.includes()&lt;/code&gt; is perfectly fine for small datasets, but &lt;code&gt;Set.has()&lt;/code&gt; is a precious tool in situations where speed is critical. The right data structure can help optimize your JavaScript applications to be faster and more efficient without making major changes to your code.&lt;/p&gt;

&lt;p&gt;Therefore, whenever you find yourself checking whether something is there, then you should ask yourself: Is that a job for an array, or am I able to use the power of a set? Getting it right might well be the difference your application needs.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
