-
Notifications
You must be signed in to change notification settings - Fork 29
/
benchmark.js
56 lines (47 loc) · 1.96 KB
/
benchmark.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import dns from 'node:dns';
import Benchmark from 'benchmark';
import CacheableLookup from './source/index.js';
const cacheable = new CacheableLookup();
const suite = new Benchmark.Suite();
const options = {
defer: true
};
const lookupOptions = {
all: true
};
const lookupOptionsADDRCONFIG = {
...lookupOptions,
hints: dns.ADDRCONFIG
};
const query = 'example.com';
suite.add('CacheableLookup#lookupAsync', deferred => {
// eslint-disable-next-line promise/prefer-await-to-then
cacheable.lookupAsync(query).then(() => deferred.resolve());
}, options).add('CacheableLookup#lookupAsync.all', deferred => {
// eslint-disable-next-line promise/prefer-await-to-then
cacheable.lookupAsync(query, lookupOptions).then(() => deferred.resolve());
}, options).add('CacheableLookup#lookupAsync.all.ADDRCONFIG', deferred => {
// eslint-disable-next-line promise/prefer-await-to-then
cacheable.lookupAsync(query, lookupOptionsADDRCONFIG).then(() => deferred.resolve());
}, options).add('CacheableLookup#lookup', deferred => {
cacheable.lookup(query, lookupOptions, () => deferred.resolve());
}, options).add('CacheableLookup#lookup.all', deferred => {
cacheable.lookup(query, lookupOptions, () => deferred.resolve());
}, options).add('CacheableLookup#lookup.all.ADDRCONFIG', deferred => {
cacheable.lookup(query, lookupOptionsADDRCONFIG, () => deferred.resolve());
}, options).add('dns#lookup', deferred => {
dns.lookup(query, () => deferred.resolve());
}, options).add('dns#lookup.all', deferred => {
dns.lookup(query, lookupOptions, () => deferred.resolve());
}, options).add('dns#lookup.all.ADDRCONFIG', deferred => {
dns.lookup(query, lookupOptionsADDRCONFIG, () => deferred.resolve());
}, options).on('cycle', event => {
console.log(String(event.target));
}).on('complete', function () {
console.log(`Fastest is ${this.filter('fastest').map('name')}`);
});
(async () => {
await cacheable.lookupAsync(query);
await new Promise(resolve => setTimeout(resolve, 150));
suite.run();
})();