Skip to content
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

chore: Replaced backtracking regex with new algorithm #2887

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

jsumners-nr
Copy link
Contributor

This PR resolves #2860. If we were to insist of using a simple regular expression to match the characters we want to strip, that regular expression would always include backtracking. This is because we want to replace all occurrences of the offending characters. The only solution I can determine that will satisfy Sonar is to split and rejoin.

@jsumners-nr jsumners-nr marked this pull request as ready for review January 16, 2025 15:07
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.35%. Comparing base (3b7e4bf) to head (4d14f2a).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2887      +/-   ##
==========================================
- Coverage   97.39%   97.35%   -0.05%     
==========================================
  Files         308      308              
  Lines       47330    47330              
==========================================
- Hits        46099    46077      -22     
- Misses       1231     1253      +22     
Flag Coverage Δ
integration-tests-cjs-18.x 73.18% <100.00%> (+<0.01%) ⬆️
integration-tests-cjs-20.x 73.17% <100.00%> (-0.01%) ⬇️
integration-tests-cjs-22.x 73.21% <100.00%> (ø)
integration-tests-esm-18.x 49.83% <0.00%> (ø)
integration-tests-esm-20.x 49.84% <0.00%> (ø)
integration-tests-esm-22.x 49.88% <0.00%> (ø)
unit-tests-18.x 89.11% <100.00%> (ø)
unit-tests-20.x 89.11% <100.00%> (ø)
unit-tests-22.x 89.12% <100.00%> (ø)
versioned-tests-18.x 79.22% <0.00%> (-0.17%) ⬇️
versioned-tests-20.x 79.20% <0.00%> (-0.19%) ⬇️
versioned-tests-22.x 79.21% <0.00%> (-0.19%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jsumners-nr
Copy link
Contributor Author

I wrote the below benchmark to evaluate the two algorithms. In my tests, the split method is consistently faster than the regex replace method. Given that, I am going to merge this PR.

/tmp/16 took 8m 15.4s
❯ node index.js
with regex duration: 2797038958
with split duration: 1204851250
regex is faster: false

/tmp/16 took 4.1s
❯ node index.js
with regex duration: 2838906166
with split duration: 1201614500
regex is faster: false

/tmp/16 took 4.1s
❯ node index.js
with regex duration: 2735240000
with split duration: 1210803583
regex is faster: false
'use strict'

const assert = require('node:assert')

const iterations = 1_000_000
const longHeaderKey = 'a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z'
const expected = 'aBCDEFGHIJKLMNOPQRSTUVWXYZ'

const aStart = process.hrtime.bigint()
for (let i = 0; i < iterations; i += 1) {
  const found = replaceWithRegex(longHeaderKey)
  assert.equal(found, expected)
}
const aEnd = process.hrtime.bigint()

const bStart = process.hrtime.bigint()
for (let i = 0; i < iterations; i += 1) {
  const found = replaceWithSplit(longHeaderKey)
  assert.equal(found, expected)
}
const bEnd = process.hrtime.bigint()

const regexDuration = aEnd - aStart
const splitDuration = bEnd - bStart
console.log(`with regex duration: ${regexDuration}`)
console.log(`with split duration: ${splitDuration}`)
console.log(`regex is faster: ${regexDuration < splitDuration}`)

function replaceWithRegex(input) {
  return input.replace(/[\W_]+(\w)/g, (m, $1) => $1.toUpperCase())
}

function replaceWithSplit(input) {
  return input.split(/[\W_]/).map((ele, i) => {
    if (i === 0) return ele
    return ele.slice(0,1).toUpperCase() + ele.slice(1)
  }).join('')
}

@jsumners-nr jsumners-nr merged commit 46462d0 into newrelic:main Jan 16, 2025
28 of 29 checks passed
@jsumners-nr jsumners-nr deleted the issue-2860 branch January 16, 2025 17:33
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.

Improve header renaming regular expression
2 participants