Rully Saputra’s Post

View profile for Rully Saputra

Mid Frontend Engineer at Tiket.com | React · TypeScript · Next.js | Core Web Vitals · Web Performance · AI-Powered Products | Ex-Traveloka

Still using plain objects as a hash table in JavaScript? 👀 We all love O(1) , right? That’s why hash tables are one of the most used data structures. But I still see many people using plain objects for this case. Yes, objects can work… but when it comes to insertion order, things get a bit tricky. It’s not always guaranteed in the way you expect, especially across different key types. That’s where Map comes in. Map is very similar to an object, but it’s designed specifically for key-value operations: - Keeps insertion order by default - Has built-in methods like set, get, delete - Performs better when you frequently add/remove keys This makes it really useful for cases like caching or else, without needing to build your own abstraction. Another interesting part is security. With plain objects, there’s a risk of prototype-related issues like object injection (just knew this scenario can be security vulnerabilities hahahahaha). Map doesn’t have this problem since it doesn’t rely on object prototypes in the same way. That said… I wouldn’t say Map always replaces objects. Objects are still simpler for static structures or JSON-like data. But for dynamic key-value operations? Map feels like the better choice. Are you still using objects for hash tables, or already switched to Map? 👀 #datastructure #tips #javascript

  • No alternative text description for this image
Yosua Petra

Software Engineer @ Sociolla | MEVN/MERN stack enjoyer | Falling deeper into the refactoring rabbit hole, one function at a time.

1mo

That's interesting. Based on your experience, where do you draw the line between using a Map and a standard Object in the context of high-throughput performance? (e.g., maybe reading cache with 1000s of ops/sec) Do you have any recent experience in real-life applications where you chose Map over Object based on performance? I still can't wrap the actual implementation of Map vs Object in the JavaScript engine, I guess I should look it up

Like
Reply
Rickvian A.

Ninja Van - Software Engineer | Fullstack | AI-augmented | TS React Golang Node

1mo

i recommend Map, esp in case where the key is not string, it has less overhead of key datatype coversion to string, and in case you need the original datatype you dont need to typecast it back

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories