From 86e0c4e40945b7849bdea83c3b7c7f023bab0fa7 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 11 Jul 2024 11:01:31 +0200 Subject: [PATCH 1/2] add benchmarks --- benchmarks/instantiate.mjs | 73 ++++++++++++++++++++++++++++++++++++++ package-lock.json | 7 ++++ package.json | 1 + 3 files changed, 81 insertions(+) create mode 100644 benchmarks/instantiate.mjs diff --git a/benchmarks/instantiate.mjs b/benchmarks/instantiate.mjs new file mode 100644 index 0000000..4fdbda0 --- /dev/null +++ b/benchmarks/instantiate.mjs @@ -0,0 +1,73 @@ +import { Bench } from 'tinybench'; +import { RequestError } from "../pkg/dist-src/index.js"; + +const bench = new Bench({ time: 100 }); + +const optionsWithSimpleRequest = Object.assign({}, { + request: { + method: "POST", + url: "https://api.github.com/authorizations", + headers: {}, + body: { + note: "test", + }, + }, + response: { + headers: {}, + status: 200, + url: "https://api.github.com/", + data: {}, + } +}); + +const optionsWithRequestHavingClientSecretInQuery = Object.assign({}, { + request: { + method: "POST", + url: "https://api.github.com/authorizations?client_secret=secret", + headers: {}, + body: { + note: "test", + }, + }, + response: { + headers: {}, + status: 200, + url: "https://api.github.com/", + data: {}, + } +}); + +const optionsWithRequestHavingAuthorizationHeader = Object.assign({}, { + request: { + method: "POST", + url: "https://api.github.com/authorizations", + headers: { + authorization: "token secret123", + }, + body: { + note: "test", + }, + }, + response: { + headers: {}, + status: 200, + url: "https://api.github.com/", + data: {}, + } +}); + +bench +.add('instantiate a simple RequestError', () => { + new RequestError("test", 123, optionsWithSimpleRequest) +}) +.add('instantiate a RequestError with authorization header in header', () => { + new RequestError("test", 123, optionsWithRequestHavingAuthorizationHeader) +}) +.add('instantiate a RequestError with client_secret in query', () => { + new RequestError("test", 123, optionsWithRequestHavingClientSecretInQuery) +}) + +await bench.warmup(); // make results more reliable, ref: https://github.com/tinylibs/tinybench/pull/50 +await bench.run(); + +console.table(bench.table()); diff --git a/package-lock.json b/package-lock.json index ab725a2..06cf8f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "glob": "^11.0.0", "jest": "^29.0.0", "prettier": "3.3.2", + "tinybench": "^2.8.0", "ts-jest": "^29.0.0", "typescript": "^5.0.0" }, @@ -4605,6 +4606,12 @@ "node": "*" } }, + "node_modules/tinybench": { + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.8.0.tgz", + "integrity": "sha512-1/eK7zUnIklz4JUUlL+658n58XO2hHLQfSk1Zf2LKieUjxidN16eKFEoDEfjHc3ohofSSqK3X5yO6VGb6iW8Lw==", + "dev": true + }, "node_modules/tmpl": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/tmpl/-/tmpl-1.0.5.tgz", diff --git a/package.json b/package.json index 6d540dd..acffb7d 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "glob": "^11.0.0", "jest": "^29.0.0", "prettier": "3.3.2", + "tinybench": "^2.8.0", "ts-jest": "^29.0.0", "typescript": "^5.0.0" }, From a9c76282833e6e5cde9d28a4eeeabf01ecb532e8 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Thu, 11 Jul 2024 11:01:51 +0200 Subject: [PATCH 2/2] remove call of captureStackTrace --- src/index.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index ffda6b2..96b3caa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,12 +29,6 @@ export class RequestError extends Error { ) { super(message); - // Maintains proper stack trace (only available on V8) - /* istanbul ignore next */ - if (Error.captureStackTrace) { - Error.captureStackTrace(this, this.constructor); - } - this.name = "HttpError"; // Implicit coercion to number if statusCode is a string