Skip to content

Commit

Permalink
Only lookup exact match if there are no startsWith matches (#7)
Browse files Browse the repository at this point in the history
* refactor: only lookup exact match if there are no startsWith matches

Optimize use of calls that doesn't need to run until they do.

* refactor: further optimize to lower bundle size

* fix: make tests pass (further lowering bundled size)
  • Loading branch information
avioli authored Dec 24, 2023
1 parent d7688b1 commit b7660b6
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ const currentProxy = {
const prefix = config.prefix ? `${config.prefix}-` : ""
const metaName = `${prefix}${propertyName}`

const exact = document.head.querySelector<HTMLMetaElement>(`meta[name=${metaName}]`)
const startsWith = Array.from(document.head.querySelectorAll<HTMLMetaElement>(`meta[name^=${metaName}-]`))
const startsWith = document.head.querySelectorAll<HTMLMetaElement>(`meta[name^=${metaName}-]`)

if (startsWith.length > 0) {
for (const { name, content } of startsWith) {
startsWith.forEach(({ name, content }) => {
const key = camelize(name.slice(metaName.length + 1))
if (result[key]) continue
if (result[key]) return
result[key] = content
}
} else if (exact) {
return exact.content
})
return result
}

return result
const exact = document.head.querySelector<HTMLMetaElement>(`meta[name=${metaName}]`)
return exact ? exact.content : result
},
}

Expand Down

0 comments on commit b7660b6

Please sign in to comment.