diff options
-rw-r--r-- | .prettierignore | 3 | ||||
-rw-r--r-- | bench/snippets/headers.mjs | 40 | ||||
-rw-r--r-- | packages/bun-types/globals.d.ts | 16 |
3 files changed, 57 insertions, 2 deletions
diff --git a/.prettierignore b/.prettierignore index ebb14dfbd..d5f16029d 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,9 +1,8 @@ src/fallback.html # src/test -bench test/bun.js/solid-dom-fixtures test/bun.js/bundled src/bun.js/builtins # src/api/demo test/snapshots -test/snapshots-no-hmr
\ No newline at end of file +test/snapshots-no-hmr diff --git a/bench/snippets/headers.mjs b/bench/snippets/headers.mjs new file mode 100644 index 000000000..ace0d9f00 --- /dev/null +++ b/bench/snippets/headers.mjs @@ -0,0 +1,40 @@ +import { bench, run } from "../node_modules/mitata/src/cli.mjs"; + +// pure JS implementation will optimze this out +bench("new Headers", function () { + return new Headers(); +}); + +var big = new Headers({ + "Content-Type": "text/plain", + "Content-Length": "123", + "X-Custom-Header": "Hello World", + "X-Another-Custom-Header": "Hello World", + "X-Yet-Another-Custom-ader": "Hello World", + "X-Yet-Another-Custom-Heder": "Hello World", + "X-Yet-Another-Custom-Heade": "Hello World", + "X-Yet-Another-Custom-Headz": "Hello Worlda", +}); + +bench("Header.get", function () { + return big.get("Content-Type"); +}); + +bench("Header.set (standard)", function () { + return big.set("Content-Type", "text/html"); +}); + +bench("Header.set (non-standard)", function () { + return big.set("X-My-Custom", "text/html123"); +}); + +if (big.toJSON) + bench("Header.toJSON", function () { + return big.toJSON(); + }); + +bench("Header fromEntries", function () { + return Object.fromEntries(big.entries()); +}); + +run(); diff --git a/packages/bun-types/globals.d.ts b/packages/bun-types/globals.d.ts index 86eb4c3d1..e3b2e6a3e 100644 --- a/packages/bun-types/globals.d.ts +++ b/packages/bun-types/globals.d.ts @@ -326,6 +326,22 @@ interface Headers { callbackfn: (value: string, key: string, parent: Headers) => void, thisArg?: any, ): void; + + /** + * Convert {@link Headers} to a plain JavaScript object. + * + * About 10x faster than `Object.fromEntries(headers.entries())` + * + * Called when you run `JSON.stringify(headers)` + * + * Does not preserve insertion order. Well-known header names are lowercased. Other header names are left as-is. + */ + toJSON(): Record<string, string>; + + /** + * Get the total number of headers + */ + readonly count: number; } declare var Headers: { |