tldts.bench.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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', 'parse'] 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. tldts[methodName](data[i], tldtsOpt);
  32. }
  33. });
  34. bench('tldts-experimental', () => {
  35. for (let i = 0, len = data.length; i < len; i++) {
  36. tldtsExperimental[methodName](data[i], tldtsOpt);
  37. }
  38. });
  39. });
  40. });
  41. run();
  42. })();