Optimize isFakeDomain(): Map-based lookup & suffix matching for 1,000x faster validations#130
Optimize isFakeDomain(): Map-based lookup & suffix matching for 1,000x faster validations#130
Conversation
Co-authored-by: guplem <11029629+guplem@users.noreply.github.com>
Co-authored-by: guplem <11029629+guplem@users.noreply.github.com>
Co-authored-by: guplem <11029629+guplem@users.noreply.github.com>
Deleted PERFORMANCE.md, benchmark.js, test-offline.js, and test-performance.js. This removes performance documentation and related test/benchmark scripts from the repository.
|
This is tackling the same problem this other PR does. |
|
@guplem you may optimize it further like I did by first making an exact match check: const normalizedDomain = domain.trim().toLowerCase()
if (json.domains[normalizedDomain]) return normalizedDomainThat way you won't even spend CPU cycles on structure setup if there's an exact match :) |
Skip Map/WeakMap cache overhead for exact matches by checking json.domains directly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Hey @GalacticHypernova, good call! You're right that a direct property lookup on That said, after looking into it a bit more, the practical benefit turns out to be pretty small. Since the WeakMap caches the Map after the first call, subsequent lookups are already just a Still, it's a clean and harmless optimization, so I went ahead and pushed it. Thanks for the suggestion! |
The
isFakeDomain()function was iterating through all +4,500 domains with regex matching on every lookup, causing ~3.2ms per lookup.Changes
Core optimization (
index.js):Mapfor O(1) exact domain matchingWeakMapcache to avoid rebuilding lookup structuresBefore:
After:
Performance
Testing
Added comprehensive test suites (
test-performance.js,test-offline.js,benchmark.js) validating: