Skip to content

Commit

Permalink
[DI] Improve performance of algorithm to find probe scriptId
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Sep 26, 2024
1 parent 37af606 commit 1c2a37c
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/dd-trace/src/debugger/devtools_client/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const session = require('./session')

const scriptIds = []
const scriptIds = new Map()
const scriptUrls = new Map()

module.exports = {
Expand All @@ -24,12 +24,18 @@ module.exports = {
* To fix this, specify a more unique file path, e.g `demo-project1/index.js` instead of `index.js`
*
* @param {string} path
* @returns {[string, string] | undefined}
* @returns {[string, string] | null}
*/
findScriptFromPartialPath (path) {
return scriptIds
.filter(([url]) => url.endsWith(path))
.sort(([a], [b]) => a.length - b.length)[0]
let matchUrl = null
let matchId = null
for (const [url, id] of scriptIds) {
if (url.endsWith(path) && (matchUrl === null || url.length < matchUrl.length)) {
matchUrl = url
matchId = id
}
}
return matchUrl === null ? null : [matchUrl, matchId]
},

getScriptUrlFromId (id) {
Expand All @@ -48,6 +54,6 @@ module.exports = {
session.on('Debugger.scriptParsed', ({ params }) => {
scriptUrls.set(params.scriptId, params.url)
if (params.url.startsWith('file:')) {
scriptIds.push([params.url, params.scriptId])
scriptIds.set(params.url, params.scriptId)
}
})

0 comments on commit 1c2a37c

Please sign in to comment.