diff --git a/lib/create-entity-finder-api.js b/lib/create-entity-finder-api.js index 1e41cc5..78ae7c8 100644 --- a/lib/create-entity-finder-api.js +++ b/lib/create-entity-finder-api.js @@ -6,8 +6,10 @@ const ROOT_DOMAIN_REGEX = /[^.]+\.([^.]+|(gov|com|co|ne)\.\w{2})$/i function getDomainFromOriginOrURL(originOrURL) { if (typeof originOrURL !== 'string') return null if (originOrURL.length > 10000 || originOrURL.startsWith('data:')) return null - if (DOMAIN_IN_URL_REGEX.test(originOrURL)) return originOrURL.match(DOMAIN_IN_URL_REGEX)[1] - if (DOMAIN_CHARACTERS.test(originOrURL)) return originOrURL.match(DOMAIN_CHARACTERS)[0] + let m = originOrURL.match(DOMAIN_IN_URL_REGEX) + if (m) return m[1] + m = originOrURL.match(DOMAIN_CHARACTERS) + if (m) return m[0] return null } diff --git a/lib/create-entity-finder-api.test.js b/lib/create-entity-finder-api.test.js index d1856f2..5f4145a 100644 --- a/lib/create-entity-finder-api.test.js +++ b/lib/create-entity-finder-api.test.js @@ -1,3 +1,5 @@ +const fs = require('fs') +const path = require('path') const {createAPIFromDataset} = require('./create-entity-finder-api.js') describe('getEntity', () => { @@ -41,4 +43,16 @@ describe('getEntity', () => { expect(api.getEntity('https://bar.example.co.uk/path').name).toEqual('Domain') expect(api.getEntity('https://baz.bar.example.co.uk/path').name).toEqual('Domain') }) + + it.skip('stress test', () => { + const urls = fs + .readFileSync(path.join(__dirname, '../data/random-urls.txt'), 'utf8') + .split('\n') + .filter(Boolean) + console.time('getEntity') + for (let i = 0; i < 1_000_000; i++) { + api.getEntity(urls[i % urls.length]) + } + console.timeEnd('getEntity') + }) })