Skip to content

Commit

Permalink
perf: avoid intermediate array (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats authored Mar 31, 2024
1 parent f426232 commit 6cc3e0d
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ const hasQueryParameter = (req, key) => {
return value !== undefined && value !== null
}

const createKey = bypassQueryParameter => ({ req }) => {
const urlObj = new URL(req.url, 'http://localhost:8080')
const OMIT_KEYS = [bypassQueryParameter, /^utm_\w+/i]
const omitKeys = Array.from(urlObj.searchParams.keys()).reduce((acc, key) => {
const isOmitable = OMIT_KEYS.some(omitQueryParam =>
omitQueryParam instanceof RegExp
? omitQueryParam.test(key)
: omitQueryParam === key
)
return isOmitable ? [...acc, key] : acc
}, [])
omitKeys.forEach(key => urlObj.searchParams.delete(key))
return [
`${urlObj.pathname}${urlObj.search}`,
hasQueryParameter(req, bypassQueryParameter)
]
}
const createKey =
bypassQueryParameter =>
({ req }) => {
const urlObj = new URL(req.url, 'http://localhost:8080')
const OMIT_KEYS = [bypassQueryParameter, /^utm_\w+/i]
Array.from(urlObj.searchParams.keys()).forEach(key => {
const isOmitable = OMIT_KEYS.some(omitQueryParam =>
omitQueryParam instanceof RegExp
? omitQueryParam.test(key)
: omitQueryParam === key
)
if (isOmitable) {
urlObj.searchParams.delete(key)
}
})

return [
`${urlObj.pathname}${urlObj.search}`,
hasQueryParameter(req, bypassQueryParameter)
]
}

const toSeconds = ms => Math.floor(ms / 1000)

Expand Down

0 comments on commit 6cc3e0d

Please sign in to comment.