When working with a list, an array is an obvious choice


When working with a list, an array is an obvious choice.

But sometimes it's preferable to use an object instead.

Here's when:
1. Each item has a unique ID
2. You plan to retrieve by ID

In this scenario, an object is more efficient and convenient.

#javascript #reactjs

My question: What do you call the pattern on the right?

Dictionary? Hash table?

Wikipedia calls it an "associative array" here: https://en.wikipedia.org/wiki/Associative_array, but I find that name confusing since it's an object.

I call it an object keyed by ID. Thoughts?
Also, I should clarify the big difference in big O notation.

Array: 𝑶(𝓃) 👎
Object: 𝑶(1) 👍

With the array, the larger it gets, the slower the lookup gets.

With the object, array size doesn't affect lookup speed. It's "direct" access, so it's always fast.

View original on X