<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/when-to-use-hashmap-instead-of-loop-tz1ltj1p1" -->

---
title: When to use HashMap instead of Loop | daily.dev
description: A short guide comparing linear search with loops versus O(1) lookup using JavaScript's Map (HashMap). Using a loop to find a user by ID has O(n) time...
canonical: https://daily.dev/posts/when-to-use-hashmap-instead-of-loop-tz1ltj1p1
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: When to use HashMap instead of Loop | daily.dev
og:description: A short guide comparing linear search with loops versus O(1) lookup using JavaScript's Map (HashMap). Using a loop to find a user by ID has O(n) time...
og:url: https://daily.dev/posts/when-to-use-hashmap-instead-of-loop-tz1ltj1p1
og:image: https://api.daily.dev/og/posts/tz1LtJ1p1.png
og:image:alt: When to use HashMap instead of Loop
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# When to use HashMap instead of Loop

**[Playful Programming](https://daily.dev/sources/playfulprogramming)** · 2 min read · 0 upvotes · 0 comments

## Summary

A short guide comparing linear search with loops versus O(1) lookup using JavaScript's Map (HashMap). Using a loop to find a user by ID has O(n) time complexity, while storing users in a Map enables constant-time O(1) lookups. The key takeaway: use a Map when frequently searching by a unique field like an ID, keeping in mind that building the Map from an array still costs O(n).

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://playfulprogramming.com/posts/when-to-use-map-instead-of-loop>

## Similar posts on daily.dev

- [The Dictionary Problem: How Do You Efficiently Look Up Things?](https://daily.dev/posts/the-dictionary-problem-how-do-you-efficiently-look-up-things--txhixcvpo) · The Polymathic Engineer · 1 upvotes · 0 comments
- [Surviving the RAM Squeeze: Efficiency Tips for JavaScript Developers](https://daily.dev/posts/surviving-the-ram-squeeze-efficiency-tips-for-javascript-developers-wdlnzmfb2) · The Miners · 37 upvotes · 7 comments
- [Arrays: From Classroom Attendance to Algorithms](https://daily.dev/posts/arrays-from-classroom-attendance-to-algorithms-mk2qcc022) · Code Like A Girl · 4 upvotes · 0 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#data-structures](https://daily.dev/tags/data-structures)

[View this post on daily.dev](https://daily.dev/posts/when-to-use-hashmap-instead-of-loop-tz1ltj1p1)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"When to use HashMap instead of Loop","url":"https://daily.dev/posts/when-to-use-hashmap-instead-of-loop-tz1ltj1p1","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/when-to-use-hashmap-instead-of-loop-tz1ltj1p1"},"datePublished":"2026-03-16T21:47:41.152Z","dateModified":"2026-03-16T22:00:04.058Z","description":"A short guide comparing linear search with loops versus O(1) lookup using JavaScript's Map (HashMap). Using a loop to find a user by ID has O(n) time...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c3646b93f87b316e9b1f610e3402ffb2?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c3646b93f87b316e9b1f610e3402ffb2?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Playful Programming","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Playful Programming","logo":"https://media.daily.dev/image/upload/s--WoGOmY81--/f_auto,q_auto/v1773697582/logos/playfulprogramming?_a=BAMAMiiu0","url":"https://daily.dev/sources/playfulprogramming"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/when-to-use-hashmap-instead-of-loop-tz1ltj1p1","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,data-structures","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Playful Programming","item":"https://daily.dev/sources/playfulprogramming"},{"@type":"ListItem","position":3,"name":"When to use HashMap instead of Loop"}]}
```

