<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>HashMap on John Jr</title>
    <link>https://johnjr.dev/tags/hashmap/</link>
    <description>Recent content in HashMap on John Jr</description>
    <generator>Hugo</generator>
    <language>en</language>
    <managingEditor>blog@johnjr.dev (John Jr)</managingEditor>
    <webMaster>blog@johnjr.dev (John Jr)</webMaster>
    <lastBuildDate>Mon, 13 Mar 2023 14:04:03 -0400</lastBuildDate>
    <atom:link href="https://johnjr.dev/tags/hashmap/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Improving the check if an object is in a Set/HashMap data structure in Ruby by using better comparisons, memoization, and object orientation practices</title>
      <link>https://johnjr.dev/posts/improving-the-check-if-an-object-is-in-a-set-hashmap-data-structure-in-ruby-by-using-better-comparisons-memoization-and-object-orientation-practices/</link>
      <pubDate>Mon, 13 Mar 2023 14:04:03 -0400</pubDate><author>blog@johnjr.dev (John Jr)</author>
      <guid>https://johnjr.dev/posts/improving-the-check-if-an-object-is-in-a-set-hashmap-data-structure-in-ruby-by-using-better-comparisons-memoization-and-object-orientation-practices/</guid>
      <description>&lt;p&gt;I was verifying the performance of an endpoint in an API using &lt;a href=&#34;https://rbspy.github.io/profiling-guide/using-flamegraphs.html&#34;&gt;framegraph&lt;/a&gt; when I noticed that checking if an object is in a set or a hash data structure was consuming a lot of time.&#xA;This post explains the problem and the solution to it.&lt;/p&gt;&#xA;&lt;p&gt;First of all, checking if an object is in a &lt;a href=&#34;https://en.wikipedia.org/wiki/Set_(abstract_data_type)&#34;&gt;Set&lt;/a&gt; or &lt;a href=&#34;https://en.wikipedia.org/wiki/Hash_table&#34;&gt;HashMap&lt;/a&gt; is &lt;code&gt;O(1)&lt;/code&gt; when we don&amp;rsquo;t consider collisions. Even in the presence of collisions, it should be much less than &lt;code&gt;O(n)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the number of items in the set. Otherwise, we are operating as a linked list.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
