Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(context): improve performance of context (setHeaders) #3592

Merged
merged 2 commits into from
Oct 31, 2024

Conversation

EdamAme-x
Copy link
Contributor

@EdamAme-x EdamAme-x commented Oct 31, 2024

Improved performance of setHeaders and removed unnecessary checks in .newResponse.

The benchmark results are as follows

In Node (1.5x~ faster)

cpu: AMD EPYC 7763 64-Core Processor
runtime: node v20.18.0 (x64-linux)

benchmark             time (avg)             (min … max)       p75       p99      p995
-------------------------------------------------------- -----------------------------
using .entries      1.19 µs/iter     (1.07 µs … 1.63 ms)   1.12 µs   2.69 µs   2.89 µs
using for syntax   707.2 ns/iter (683.65 ns … 948.47 ns) 714.47 ns 948.47 ns 948.47 ns

In Deno (4x~ faster)

cpu: AMD EPYC 7763 64-Core Processor
runtime: deno 2.0.4 (x86_64-unknown-linux-gnu)

benchmark             time (avg)             (min … max)       p75       p99      p995
-------------------------------------------------------- -----------------------------
using .entries      3.83 µs/iter     (2.44 µs … 2.88 ms)   3.48 µs   7.12 µs   7.65 µs
using for syntax   893.5 ns/iter   (836.79 ns … 1.32 µs) 881.82 ns   1.32 µs   1.32 µs

In Bun (1.5x~ faster)

cpu: AMD EPYC 7763 64-Core Processor
runtime: bun 1.1.33 (x64-linux)

benchmark             time (avg)             (min … max)       p75       p99      p995
-------------------------------------------------------- -----------------------------
using .entries    771.93 ns/iter   (704.77 ns … 1.25 µs) 788.32 ns   1.25 µs   1.25 µs
using for syntax   533.1 ns/iter   (440.84 ns … 1.89 µs) 522.99 ns 945.47 ns   1.89 µs

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Oct 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.71%. Comparing base (caeecf9) to head (67b2b59).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3592      +/-   ##
==========================================
- Coverage   94.71%   94.71%   -0.01%     
==========================================
  Files         158      158              
  Lines        9557     9555       -2     
  Branches     2826     2924      +98     
==========================================
- Hits         9052     9050       -2     
  Misses        505      505              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EdamAme-x
Copy link
Contributor Author

Benchmark Source

import { run, bench } from 'mitata'

const subHeaders = {
  a: '1',
  b: '2',
  c: '3',
  d: '4',
  e: '5',
  f: '6',
}

bench('using .entries', () => {
  const headers = new Headers()

  Object.entries(subHeaders).forEach(([key, value]) => headers.set(key, value))
})

bench('using for syntax', () => {
  const headers = new Headers()

  for (let i = 0, keys = Object.keys(subHeaders), len = keys.length; i < len; i++) {
    const key = keys[i]
    headers.set(key, subHeaders[key])
  }
})

await run()

src/context.ts Outdated Show resolved Hide resolved
@yusukebe
Copy link
Member

@EdamAme-x

Thanks!

@yusukebe yusukebe changed the title perf(context): improve performance of context (newResponse, setHeaders) perf(context): improve performance of context (setHeaders) Oct 31, 2024
@yusukebe yusukebe merged commit 4dd8b2b into honojs:main Oct 31, 2024
16 checks passed
TinsFox pushed a commit to TinsFox/hono that referenced this pull request Nov 11, 2024
* perf(context): improve performance of context (newResponse, setHeaders)

* Revert context.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants