Skip to content

Conversation

Uzlopak
Copy link
Contributor

@Uzlopak Uzlopak commented Aug 28, 2025

sonarjs was reporting some imperformance in the regexes

This relates to...

Rationale

Changes

Features

Bug Fixes

Breaking Changes and Deprecations

Status

@Uzlopak Uzlopak requested a review from Copilot September 6, 2025 06:38
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves regex patterns in the data-url.js file to address performance issues identified by SonarJS. The changes focus on optimizing regex patterns and refactoring a for loop to a while loop.

  • Adds the u flag to regex patterns for better Unicode handling and performance
  • Optimizes character class ordering and grouping in regex patterns
  • Refactors a for loop to a while loop in the percentDecode function

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@@ -86,7 +86,7 @@ function dataURLProcessor (dataURL) {

// 5. Remove trailing U+0020 SPACE code points from mimeType,
// if any.
mimeType = mimeType.replace(/(\u0020)+$/, '')
mimeType = mimeType.replace(/(\u0020+)$/u, '')
Copy link
Preview

Copilot AI Sep 6, 2025

Choose a reason for hiding this comment

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

The capturing group (\u0020+) is unnecessary since $1 is not used in the replacement. Use a non-capturing group instead: /(?:\u0020+)$/u or simply /\u0020+$/u

Suggested change
mimeType = mimeType.replace(/(\u0020+)$/u, '')
mimeType = mimeType.replace(/\u0020+$/u, '')

Copilot uses AI. Check for mistakes.

@@ -572,7 +574,7 @@ function serializeAMimeType (mimeType) {
if (!HTTP_TOKEN_CODEPOINTS.test(value)) {
// 1. Precede each occurrence of U+0022 (") or
// U+005C (\) in value with U+005C (\).
value = value.replace(/(\\|")/g, '\\$1')
value = value.replace(/[\\"]/ug, '\\$&')
Copy link
Preview

Copilot AI Sep 6, 2025

Choose a reason for hiding this comment

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

The g flag is redundant when combined with the u flag for this specific pattern. The u flag alone is sufficient for Unicode support, and g should only be used when multiple replacements are needed.

Copilot uses AI. Check for mistakes.

@metcoder95 metcoder95 requested a review from tsctx September 7, 2025 08:47
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.

2 participants