Skip to content

perf: Utilize Maps for metric collection - #658

Merged
zbjornson merged 3 commits into
prometheus:masterfrom
jdmarshall:performance
Jun 27, 2025
Merged

zbjornson merged 3 commits into
prometheus:masterfrom
jdmarshall:performance

Conversation

@jdmarshall

@jdmarshall jdmarshall commented Jun 24, 2025 •

Copy link
Copy Markdown
Contributor

As of sometime around the middle of Node 16, the performance of Map for long-lived, mutable objects exceeded that of {}. I believe this gap is more pronounced in Node 18 as well.

The deal is that the representation for Objects that repeatedly change turns into a weird chain that occupies (in my benchmarks at least) ~40% more memory. So once v8 made Map.set/get faster than map[], we all should have switched to Maps and Sets.

Between the slightly faster functions and the lower memory pressure this can create a notable improvement.

Making a change like this on my last project for a long lived cache resulted in much better memory behavior and also about a 5% improvement in TTFB - just from changing a popular lookup table.

Note that the final change in this PR constitutes a change for anyone making their own subclasses, and therefore is probably breaking. Let me know if you want me to split this into two PRs, but that's where the bulk of the changes show up. The Registry just doesn't change frequently enough.

// removed hilariously optimistic perf run numbers. Most of the gains come in subsequent PRs.

@SimenB

SimenB commented Jun 24, 2025 •

Copy link
Copy Markdown
Contributor

Thanks for digging into it!

I'm fine with making a major release dropping node 16&18 and this API change 👍

Comment thread package.json Resolved
@jdmarshall

Copy link
Copy Markdown
Contributor Author

This still works on node 16. It’s just that the numbers are less impressive.

@jdmarshall
jdmarshall force-pushed the performance branch 2 times, most recently from 9f26080 to 27b4f7f Compare June 25, 2025 05:30
@jdmarshall

Copy link
Copy Markdown
Contributor Author

I got warnings about a CI error as email, but I'm not seeing them here in the PR. Shouldn't they be listed here as well?

@zbjornson

Copy link
Copy Markdown
Contributor

I just approved the workflow run. Do you see the errors here now?

Looks good to me once the two failed checks are resolved.

@jdmarshall

Copy link
Copy Markdown
Contributor Author

Sure. Are you running different eslint settings that “test”? Thought I already caught these.

@zbjornson

Copy link
Copy Markdown
Contributor

@jdmarshall

Copy link
Copy Markdown
Contributor Author

$ npm run lint

[email protected] lint
eslint .

=============

WARNING: You are currently running a version of TypeScript which is not officially supported by @typescript-eslint/typescript-estree.

You may find that it works just fine, or you may not.

SUPPORTED TYPESCRIPT VERSIONS: >=4.3.5 <5.4.0

YOUR TYPESCRIPT VERSION: 5.8.3

Please only submit bug reports when using the officially supported version.

=============

I'm getting absolutely nothing.

As of Node ~16, maps are constently faster than DIY maps, particularly
so with objects that will grow or shrink over their lifetimes.

On another project I saw an almost 40% reduction in heap size for
a large lookup table and about a 5% reduction in response time for
the entire workflow (not just the cache)
Comment thread lib/util.js Resolved
@zbjornson

Copy link
Copy Markdown
Contributor

One last CI error to fix.

These are faster than constantly changing {} instances since
sometime in Node 16 and got faster still in Node 18
@jdmarshall

Copy link
Copy Markdown
Contributor Author

Well that one was just sloppiness. Fixed.

@zbjornson zbjornson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks!

@zbjornson
zbjornson merged commit 657223e into prometheus:master Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants