DIAN.AFRIAL

#2 Javascript Fundamental: Stop Using Array.find() for Large Datasets? I Ran a Benchmark and the Results Were Wild

20 May 2026 | 20:34

Javascript

This case study compares the performance of Map.get() vs Array.find() for element lookups across datasets of varying sizes. The results clearly demonstrate that Map lookups are orders of magnitude faster than Array find operations, especially as the dataset grows

As JavaScript developers, we've all written code like this:

const user = users.find(u => u.id === targetId);

Simple, readable, and completely fine.

But recently I was working on a project where lookups happened very frequently. The dataset wasn't huge at first, but it got me wondering:

At what point should we stop using Array.find() and switch to Map.get()?

Instead of guessing, I decided to benchmark it.


The Test

The goal was simple:

Find a user by ID from datasets of different sizes:

  • 1,000 rows
  • 10,000 rows
  • 100,000 rows
  • 1,000,000 rows

Each lookup was executed 10,000 times using random IDs to avoid cache-friendly behavior.

The comparison:

Array.find()

const user = users.find(u => u.id === targetId);

Map.get()

const userMap = new Map(users.map(u => [u.id, u]));
const user = userMap.get(targetId);

Environment:

  • Bun.js runtime
  • performance.now()
  • 10,000 iterations per test
  • Random ID lookups

Benchmark Results

Dataset: 1,000 Rows

MethodOps/Second
Map.get()11,146,557
Array.find()566,377

Map was already about 20x faster.

At this size both approaches feel instant, but the difference is already visible.


Dataset: 10,000 Rows

MethodOps/Second
Map.get()4,265,731
Array.find()51,982

Now Map becomes roughly 82x faster.

This is where things start getting interesting.


Dataset: 100,000 Rows

MethodOps/Second
Map.get()2,217,355
Array.find()2,218

Map is now about 1,000x faster.

Yes, one thousand times.

This is the point where the algorithmic complexity starts showing up very clearly.


Dataset: 1,000,000 Rows

MethodOps/Second
Map.get()621,435
Array.find()128

Map is approximately 4,850x faster.

That number surprised me too.


Why Does This Happen?

The answer is hidden in Big-O complexity.

Array.find()

users.find(...)

Complexity:

O(n)

JavaScript must scan elements one by one until it finds a match.

If the target happens to be near the end of the array, the runtime grows with dataset size.


Map.get()

userMap.get(id)

Complexity:

O(1)

The lookup uses a hash table internally.

Whether your Map contains:

  • 1,000 records
  • 100,000 records
  • 1,000,000 records

the lookup time stays nearly constant.


But Creating the Map Has a Cost

One thing people often forget:

const userMap = new Map(users.map(u => [u.id, u]));

Building the Map isn't free.

My benchmark showed:

DatasetMap Construction Time
1,0001.56 ms
10,0002.32 ms
100,00043.51 ms
1,000,0001471.14 ms

For one million records, building the Map took around 1.47 seconds.

At first glance that sounds expensive.

But remember:

You build it once.

Then every lookup becomes dramatically faster.


Real-World Example

Imagine an API endpoint that needs to resolve users repeatedly:

orders.map(order => {
  return users.find(
    user => user.id === order.userId
  );
});

Looks harmless.

But if:

  • users = 100,000
  • orders = 50,000

you are repeatedly scanning the same array thousands of times.

A better approach:

const userMap = new Map(
  users.map(user => [user.id, user])
);

orders.map(order => {
  return userMap.get(order.userId);
});

Now every lookup is constant time.

This pattern appears everywhere:

  • API aggregation
  • React state normalization
  • Permission lookups
  • Product catalogs
  • Caching layers
  • Backend services

When Should You Use Array.find()?

Honestly?

Most of the time it's still perfectly fine.

I would use Array.find() when:

  • Dataset is small
  • Lookup happens rarely
  • Readability matters more than optimization
  • Performance isn't a bottleneck

Example:

const country = countries.find(
  c => c.code === "ID"
);

No need to over-engineer this.


When Should You Use Map?

I'd strongly consider Map when:

  • Lookups happen frequently
  • Dataset is large
  • The same collection is queried repeatedly
  • Performance matters

Example:

const userMap = new Map(
  users.map(u => [u.id, u])
);

Especially for backend services and data-heavy applications.


The Biggest Takeaway

The benchmark confirmed something many developers already know theoretically:

Algorithmic complexity eventually beats convenience.

At 1,000 rows the difference was about 20x.

At 1,000,000 rows the difference exploded to nearly 4,850x.

That's not a micro-optimization anymore.

That's the difference between an application feeling instant and one struggling under load.

So next time you find yourself calling Array.find() inside loops, reducers, or repeated lookups, it may be worth asking:

Should this be a Map instead?

Sometimes a small data structure change can deliver a massive performance win.