Skip to content

Commit ba04ffa

Browse files
authored
fix: improve perf of request error instantiations (#444)
* add benchmarks * remove call of captureStackTrace
1 parent 7d850d2 commit ba04ffa

File tree

4 files changed

+1099
-1039
lines changed

4 files changed

+1099
-1039
lines changed

benchmarks/instantiate.mjs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { Bench } from 'tinybench';
2+
import { RequestError } from "../pkg/dist-src/index.js";
3+
4+
const bench = new Bench({ time: 100 });
5+
6+
const optionsWithSimpleRequest = Object.assign({}, {
7+
request: {
8+
method: "POST",
9+
url: "https://api.github.com/authorizations",
10+
headers: {},
11+
body: {
12+
note: "test",
13+
},
14+
},
15+
response: {
16+
headers: {},
17+
status: 200,
18+
url: "https://api.github.com/",
19+
data: {},
20+
}
21+
});
22+
23+
const optionsWithRequestHavingClientSecretInQuery = Object.assign({}, {
24+
request: {
25+
method: "POST",
26+
url: "https://api.github.com/authorizations?client_secret=secret",
27+
headers: {},
28+
body: {
29+
note: "test",
30+
},
31+
},
32+
response: {
33+
headers: {},
34+
status: 200,
35+
url: "https://api.github.com/",
36+
data: {},
37+
}
38+
});
39+
40+
const optionsWithRequestHavingAuthorizationHeader = Object.assign({}, {
41+
request: {
42+
method: "POST",
43+
url: "https://api.github.com/authorizations",
44+
headers: {
45+
authorization: "token secret123",
46+
},
47+
body: {
48+
note: "test",
49+
},
50+
},
51+
response: {
52+
headers: {},
53+
status: 200,
54+
url: "https://api.github.com/",
55+
data: {},
56+
}
57+
});
58+
59+
bench
60+
.add('instantiate a simple RequestError', () => {
61+
new RequestError("test", 123, optionsWithSimpleRequest)
62+
})
63+
.add('instantiate a RequestError with authorization header in header', () => {
64+
new RequestError("test", 123, optionsWithRequestHavingAuthorizationHeader)
65+
})
66+
.add('instantiate a RequestError with client_secret in query', () => {
67+
new RequestError("test", 123, optionsWithRequestHavingClientSecretInQuery)
68+
})
69+
70+
await bench.warmup(); // make results more reliable, ref: https://github.com/tinylibs/tinybench/pull/50
71+
await bench.run();
72+
73+
console.table(bench.table());

0 commit comments

Comments
 (0)