Skip to content

refactor(metrics): optimize addDimensions method to avoid O(n²) complexity #4156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Jul 8, 2025

Summary

Changes

Please provide a summary of what's being changed

Replace Object.assign usage in dimensionSets reducer with direct property assignment to improve time complexity from O(n²) to O(n). The previous implementation copied all accumulator properties on each iteration, while the new approach mutates the accumulator directly using a for...of loop.

This change maintains identical behavior while significantly improving performance for metrics with multiple dimension sets, especially in high-throughput Lambda functions.

This PR addresses a performance bottleneck in the Metrics utility where Object.assign was used in a reducer pattern, causing O(n²) time complexity. The fix follows the established coding guidelines that recommend avoiding Object.assign in accumulators and using for...of loops instead.

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: Fixes #4155


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/XS PR between 0-9 LOC label Jul 8, 2025
@boring-cyborg boring-cyborg bot added the metrics This item relates to the Metrics Utility label Jul 8, 2025
Copy link

sonarqubecloud bot commented Jul 8, 2025

@dreamorosi dreamorosi self-assigned this Jul 8, 2025
@dreamorosi dreamorosi requested a review from svozza July 8, 2025 22:45
@dreamorosi dreamorosi changed the title refactor(metrics): optimize dimensionSets reducer to avoid O(n²) complexity refactor(metrics): optimize addDimensions method to avoid O(n²) complexity Jul 8, 2025
@dreamorosi dreamorosi merged commit 3982b4a into main Jul 9, 2025
54 checks passed
@dreamorosi dreamorosi deleted the feat/metrics-optimize-dimension-sets-reducer branch July 9, 2025 08:20
Copy link
Contributor

github-actions bot commented Jul 9, 2025

@aws-powertools/lambda-typescript No related issues found. Please ensure 'pending-release' label is applied before releasing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metrics This item relates to the Metrics Utility size/XS PR between 0-9 LOC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintenance: Optimize Metrics dimensionSets reducer to avoid O(n²) time complexity
2 participants