tldts.bench.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { fetchRemoteTextByLine } from './fetch-text-by-line';
  2. import { processLineFromReadline } from './process-line';
  3. import { bench, group, run } from 'mitata';
  4. import * as tldts from 'tldts';
  5. import * as tldtsExperimental from 'tldts-experimental';
  6. import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
  7. (async () => {
  8. const data = await processLineFromReadline(await fetchRemoteTextByLine('https://osint.digitalside.it/Threat-Intel/lists/latestdomains.txt'));
  9. const gorhill = await getGorhillPublicSuffixPromise();
  10. const tldtsOpt: Parameters<typeof tldts.getDomain>[1] = {
  11. allowPrivateDomains: false,
  12. extractHostname: false,
  13. validateHostname: false,
  14. detectIp: false,
  15. mixedInputs: false
  16. };
  17. (['getDomain', 'getPublicSuffix', 'getSubdomain'] as const).forEach(methodName => {
  18. group(methodName, () => {
  19. if (methodName in gorhill) {
  20. bench('gorhill', () => {
  21. for (let i = 0, len = data.length; i < len; i++) {
  22. const line = data[i];
  23. const safeGorhillLine = line[0] === '.' ? line.slice(1) : line;
  24. // @ts-expect-error -- type guarded
  25. gorhill[methodName](safeGorhillLine);
  26. }
  27. });
  28. }
  29. bench('tldts', () => {
  30. for (let i = 0, len = data.length; i < len; i++) {
  31. // eslint-disable-next-line import-x/namespace -- safe
  32. tldts[methodName](data[i], tldtsOpt);
  33. }
  34. });
  35. bench('tldts-experimental', () => {
  36. for (let i = 0, len = data.length; i < len; i++) {
  37. // eslint-disable-next-line import-x/namespace -- safe
  38. tldtsExperimental[methodName](data[i], tldtsOpt);
  39. }
  40. });
  41. });
  42. });
  43. run();
  44. })();