<?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: Mehedi Hasan</title>
    <description>The latest articles on DEV Community by Mehedi Hasan (@mehedibangladeshi).</description>
    <link>https://dev.to/mehedibangladeshi</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%2F590784%2F150c9aad-44d2-489b-bd85-5185e70c165c.jpg</url>
      <title>DEV Community: Mehedi Hasan</title>
      <link>https://dev.to/mehedibangladeshi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mehedibangladeshi"/>
    <language>en</language>
    <item>
      <title>Why Map Lookups Are Slower Than Object Lookups in JavaScript</title>
      <dc:creator>Mehedi Hasan</dc:creator>
      <pubDate>Thu, 30 Oct 2025 04:58:05 +0000</pubDate>
      <link>https://dev.to/mehedibangladeshi/why-map-lookups-are-slower-than-object-lookups-in-javascript-33g8</link>
      <guid>https://dev.to/mehedibangladeshi/why-map-lookups-are-slower-than-object-lookups-in-javascript-33g8</guid>
      <description>&lt;p&gt;Imagine this: you’re optimizing your JavaScript code and you notice something odd. You’re using a &lt;code&gt;Map&lt;/code&gt; to store some configuration settings or feature flags, but when you benchmark it against a plain object, it feels slower.&lt;/p&gt;

&lt;p&gt;Both &lt;code&gt;Map&lt;/code&gt; and &lt;code&gt;Object&lt;/code&gt; provide O(1) lookups, so what’s happening under the hood? Let’s break it down.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Common Misconception
&lt;/h2&gt;

&lt;p&gt;Many developers assume that &lt;code&gt;Map&lt;/code&gt; is the modern, better alternative to &lt;code&gt;Object&lt;/code&gt; for all key-value storage. And for certain cases, it absolutely is.&lt;/p&gt;

&lt;p&gt;But here’s the thing: if your keys are strings and the set of keys is relatively small and fixed, an &lt;code&gt;Object&lt;/code&gt; is almost always faster. Why? Let’s dive into the mechanics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Objects vs. Maps: The Basics
&lt;/h2&gt;

&lt;p&gt;Consider this scenario: you have a set of API endpoints your application uses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Using an Object
const endpoints = {
  login: '/api/login',
  logout: '/api/logout',
  profile: '/api/profile'
};

// Using a Map
const endpointsMap = new Map([
  ['login', '/api/login'],
  ['logout', '/api/logout'],
  ['profile', '/api/profile']
]);

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

&lt;/div&gt;



&lt;p&gt;Accessing a value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;endpoints['login'];       // Object property access
endpointsMap.get('login'); // Map lookup

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

&lt;/div&gt;



&lt;p&gt;On the surface, both do the same thing: give you the value for a key. But how they achieve it internally is different.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Happens Under the Hood
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Objects use hidden classes
&lt;/h3&gt;

&lt;p&gt;Modern JavaScript engines, like V8, optimize objects heavily. When you create an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const endpoints = { login: '/api/login', logout: '/api/logout' };

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The engine builds a &lt;code&gt;hidden class&lt;/code&gt; for the object — think of it as a lightweight blueprint, like a C struct.&lt;/li&gt;
&lt;li&gt;Each property gets a &lt;code&gt;fixed memory offset&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Accessing a property is basically a &lt;code&gt;direct pointer lookup&lt;/code&gt;. Blazing fast.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Maps use generic hash tables
&lt;/h3&gt;

&lt;p&gt;When you create a Map:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const endpointsMap = new Map([['login', '/api/login'], ['logout', '/api/logout']]);

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The engine can’t assume fixed property types.&lt;/li&gt;
&lt;li&gt;Keys can be anything — strings, numbers, objects, even &lt;code&gt;NaN&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Every &lt;code&gt;.get()&lt;/code&gt; involves:

&lt;ol&gt;
&lt;li&gt;Hashing the key.&lt;/li&gt;
&lt;li&gt;Finding the appropriate bucket.&lt;/li&gt;
&lt;li&gt;Comparing keys for equality.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Even though this is still O(1) on average, the constant factor is higher. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Seeing the Difference in a Benchmark
&lt;/h2&gt;

&lt;p&gt;Let’s test it with a simple benchmark:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
  login: '/api/login',
  logout: '/api/logout',
  profile: '/api/profile'
};

const map = new Map(Object.entries(obj));
const keys = ['login', 'logout', 'profile'];

console.time('Object');
for (let i = 0; i &amp;lt; 1e7; i++) obj[keys[i % 3]];
console.timeEnd('Object');

console.time('Map');
for (let i = 0; i &amp;lt; 1e7; i++) map.get(keys[i % 3]);
console.timeEnd('Map');

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

&lt;/div&gt;



&lt;p&gt;Typical results on Node.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Object: 50 ms
Map:    180 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though both are O(1), the object access is significantly faster in practice because of engine optimizations.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Map Is Actually Better
&lt;/h2&gt;

&lt;p&gt;That said, &lt;code&gt;Map&lt;/code&gt; isn’t bad — it’s just optimized for different scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Non-string keys:&lt;/strong&gt; Like objects or functions.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userRoles = new Map();
userRoles.set(userObj, 'admin');

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic key sets:&lt;/strong&gt; When keys are added or removed frequently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Guaranteed iteration order:&lt;/strong&gt; Maps preserve insertion order.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Large datasets with unknown keys:&lt;/strong&gt; Maps scale better than objects when used as true hash tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Quick Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;Object&lt;/th&gt;
&lt;th&gt;Map&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Key Types&lt;/td&gt;
&lt;td&gt;Strings / Symbols&lt;/td&gt;
&lt;td&gt;Any value&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lookup Speed&lt;/td&gt;
&lt;td&gt;Faster (hidden classes)&lt;/td&gt;
&lt;td&gt;Slower (hash + compare)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory&lt;/td&gt;
&lt;td&gt;Lower&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dynamic Resizing&lt;/td&gt;
&lt;td&gt;Poor&lt;/td&gt;
&lt;td&gt;Excellent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ideal Use Case&lt;/td&gt;
&lt;td&gt;Small, static, string-keyed data&lt;/td&gt;
&lt;td&gt;Dynamic or complex key-value pairs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Object&lt;/code&gt; lookups are JIT-optimized (hidden classes + inline caching).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Map&lt;/code&gt; lookups are generic hash table operations — flexible but slightly slower.&lt;/li&gt;
&lt;li&gt;For small, fixed, string-keyed datasets → &lt;strong&gt;Object wins.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;For dynamic or complex keys → &lt;strong&gt;Map shines.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Closing Thoughts
&lt;/h2&gt;

&lt;p&gt;Sometimes, the “modern” API isn’t the fastest — it’s just more flexible.&lt;br&gt;
Understanding what happens under the hood helps you write code that is both clean and performant.&lt;/p&gt;

&lt;p&gt;So next time you reach for &lt;code&gt;Map&lt;/code&gt; by default, ask yourself: do I really need its flexibility, or is a simple &lt;code&gt;object&lt;/code&gt; enough?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>hashmap</category>
      <category>programming</category>
      <category>softwareengineering</category>
    </item>
    <item>
      <title>Why JavaScript is so Popular?</title>
      <dc:creator>Mehedi Hasan</dc:creator>
      <pubDate>Mon, 06 Mar 2023 05:25:03 +0000</pubDate>
      <link>https://dev.to/mehedibangladeshi/why-javascript-is-so-popular-5e5m</link>
      <guid>https://dev.to/mehedibangladeshi/why-javascript-is-so-popular-5e5m</guid>
      <description>&lt;p&gt;Are you curious about why JavaScript has become one of the most popular programming languages to this day? If you've been programming for a while, you may have experienced a love-hate relationship with JavaScript at some point. I remember feeling this way when I first started programming back in 2014-2013. Python was my first love, and I just couldn't understand why people liked JavaScript so much. But fast forward to today, and all I do is write JS code. It's my go-to language, and I even write pseudo code in JS now. &lt;br&gt;
Today, we'll explore why JavaScript is still one of the most widely used programming languages and what makes it so popular among developers. Let's dive in!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;runs on browsers:&lt;/strong&gt; JS is the programming language that runs on browsers. So, It's a essential part of wed-development.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Huge community and eco-system:&lt;/strong&gt; has huge community that builds library, framework and tools for the ecosystem. which makes development easier and faster for everyone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Versatility:&lt;/strong&gt; Not just web development, it can be used to build server-side, desktop applications and even mobile applications etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easy to learn:&lt;/strong&gt; it has low learning curve compared to other popular languages. simpler syntax and easy setup makes it more accessible for beginners.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constantly evolving:&lt;/strong&gt; it's a dynamic language, hence, constantly evolving and improving. new features and improvements are being added frequently. So, it's becoming more and more powerful.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the last decade, JavaScript has become incredibly popular, largely due to its large community, versatility, and beginner-friendly nature. As web development became a more popular career choice with the growth of the internet, andJavaScript's popularity grew with it.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>todayilearned</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
