Skip to content

Conversation

@43081j
Copy link
Contributor

@43081j 43081j commented Aug 20, 2025

Does a couple of things:

  • Switches to a regular for...in which is much faster than
    Object.entries
  • Only retrieves the val if the key is valid
  • Compares keys against "" (empty string) rather than accessing
    length
  • Reuses typeof result rather than recomputing it each time

Type of change

Performance improvement

How Has This Been Tested?

Existing unit tests cover this code, and pass successfully.

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

Does a couple of things:

- Switches to a regular `for...in` which is much faster than
  `Object.entries`
- Only retrieves the `val` if the key is valid
- Compares keys against `""` (empty string) rather than accessing
  `length`
- Reuses `typeof` result rather than recomputing it each time
@43081j 43081j requested a review from a team as a code owner August 20, 2025 14:44
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Aug 20, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link

codecov bot commented Aug 20, 2025

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.99%. Comparing base (91ff198) to head (25b00e4).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...ckages/opentelemetry-core/src/common/attributes.ts 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5866      +/-   ##
==========================================
- Coverage   95.00%   94.99%   -0.01%     
==========================================
  Files         313      313              
  Lines        8771     8775       +4     
  Branches     1883     1884       +1     
==========================================
+ Hits         8333     8336       +3     
- Misses        438      439       +1     
Files with missing lines Coverage Δ
...ckages/opentelemetry-core/src/common/attributes.ts 91.66% <92.30%> (-1.52%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@raphael-theriault-swi
Copy link
Member

Could you provide some comparative figures given this is for perf ?

@43081j
Copy link
Contributor Author

43081j commented Aug 20, 2025

here's the results from a quick benchmark locally:

benchmark                   avg (min … max) p75 / p99    (min … top 1%)
------------------------------------------- -------------------------------
sanitizeAttributes           114.91 ns/iter 115.67 ns   ▅█
                    (109.01 ns … 186.76 ns) 135.03 ns   ██
                    (110.75  b … 611.93  b) 400.31  b ▁▁███▃▃▄▅▃▂▂▂▂▁▁▁▁▁▁▁

sanitizeAttributesMain       154.84 ns/iter 156.72 ns  █
                    (150.32 ns … 251.54 ns) 170.29 ns  █▃
                    (821.69  b …   2.59 kb)   1.00 kb ▆██▄▃▃▄▄▃▂▂▃▃▂▁▂▁▁▁▁▁

summary
  sanitizeAttributes
   1.35x faster than sanitizeAttributesMain

to be honest though, a large amount of the time is lost to warn calls 🤔

i wonder if you could optimise the log proxy to compute the logger up front?

basically this:

      const logger = getGlobal('diag');
      if (!logger) return noop;
      const logFn = logger[funcName];
      return logFn;

but it'd mean if you mutate globalThis later to add log functions, they wouldn't be picked up. so maybe not

Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

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

Thanks for the work! LGTM % a nit.

@david-luna
Copy link
Contributor

@43081j thanks for your contribution. I think this change needs an entry into CAHNGELOG.md file

@43081j
Copy link
Contributor Author

43081j commented Oct 14, 2025

i've added an entry under "internal" if that works

@pichlermarc pichlermarc added this pull request to the merge queue Oct 15, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to a conflict with the base branch Oct 15, 2025
@43081j
Copy link
Contributor Author

43081j commented Oct 15, 2025

we had a conflict from another merged PR, so I've caught the branch up again FYI

@pichlermarc pichlermarc added this pull request to the merge queue Oct 15, 2025
Merged via the queue into open-telemetry:main with commit 56f326a Oct 15, 2025
25 checks passed
@otelbot
Copy link
Contributor

otelbot bot commented Oct 15, 2025

Thank you for your contribution @43081j! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

@43081j 43081j deleted the perfttributes branch October 15, 2025 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants