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

[DI] Improve performance of algorithm to find probe scriptId #4729

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
Loading