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(ext/request): optimize Request constructor #20141

Merged
merged 1 commit into from
Aug 12, 2023

Conversation

marcosc90
Copy link
Contributor

@marcosc90 marcosc90 commented Aug 12, 2023

This PR optimizes Request constructor when init is not empty. This path is also used by fetch when options argument is used

fetch("https://deno.land", {
  method: "POST",
  body: 'land'
});
  • Removed 3 extra calls to headerListFromHeaders
  • Avoid Object.keys & headerList clone if init.headers is set
  • Only empty headersList (.splice) if it's not already empty.

Benchmarks

this patch

cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark                    time (avg)        iter/s             (min … max)       p75       p99      p995
----------------------------------------------------------------------------- -----------------------------
Request without headers       1.86 µs/iter     536,440.7     (1.67 µs … 2.76 µs)   1.89 µs   2.76 µs   2.76 µs
Request with headers          1.96 µs/iter     509,440.5     (1.83 µs … 2.17 µs)   1.99 µs   2.17 µs   2.17 µs

main

cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark                    time (avg)        iter/s             (min … max)       p75       p99      p995
----------------------------------------------------------------------------- -----------------------------
Request without headers       1.96 µs/iter     510,201.5     (1.81 µs … 2.64 µs)      2 µs   2.64 µs   2.64 µs
Request with headers          2.03 µs/iter     493,526.6     (1.84 µs … 2.31 µs)   2.08 µs   2.31 µs   2.31 µs
Deno.bench("Request without headers", () => {
  const r = new Request("https://deno.land", {
    method: "POST",
    body: '{"foo": "bar"}',
  });
});

Deno.bench("Request with headers", () => {
  const r = new Request("https://deno.land", {
    method: "POST",
    body: '{"foo": "bar"}',
    headers: {
      "Content-Type": "application/json",
    },
  });
});

Copy link
Contributor

@mmastrac mmastrac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mmastrac mmastrac merged commit 396498b into denoland:main Aug 12, 2023
12 checks passed
@marcosc90 marcosc90 deleted the perf-request-init branch August 12, 2023 16:42
littledivy pushed a commit to littledivy/deno that referenced this pull request Aug 21, 2023
This PR optimizes `Request` constructor when `init` is not empty. This
path is also used by `fetch` when `options` argument is used
```js
fetch("https://deno.land", {
  method: "POST",
  body: 'land'
});
```

- Removed 3 extra calls to `headerListFromHeaders`
- Avoid `Object.keys` & `headerList` clone if `init.headers` is set
- Only empty `headersList` (`.splice`) if it's not already empty. 

## Benchmarks

**this patch**
```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark                    time (avg)        iter/s             (min … max)       p75       p99      p995
----------------------------------------------------------------------------- -----------------------------
Request without headers       1.86 µs/iter     536,440.7     (1.67 µs … 2.76 µs)   1.89 µs   2.76 µs   2.76 µs
Request with headers          1.96 µs/iter     509,440.5     (1.83 µs … 2.17 µs)   1.99 µs   2.17 µs   2.17 µs
```

**main**

```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark                    time (avg)        iter/s             (min … max)       p75       p99      p995
----------------------------------------------------------------------------- -----------------------------
Request without headers       1.96 µs/iter     510,201.5     (1.81 µs … 2.64 µs)      2 µs   2.64 µs   2.64 µs
Request with headers          2.03 µs/iter     493,526.6     (1.84 µs … 2.31 µs)   2.08 µs   2.31 µs   2.31 µs
```

```js
Deno.bench("Request without headers", () => {
  const r = new Request("https://deno.land", {
    method: "POST",
    body: '{"foo": "bar"}',
  });
});

Deno.bench("Request with headers", () => {
  const r = new Request("https://deno.land", {
    method: "POST",
    body: '{"foo": "bar"}',
    headers: {
      "Content-Type": "application/json",
    },
  });
});
```
littledivy pushed a commit that referenced this pull request Aug 21, 2023
This PR optimizes `Request` constructor when `init` is not empty. This
path is also used by `fetch` when `options` argument is used
```js
fetch("https://deno.land", {
  method: "POST",
  body: 'land'
});
```

- Removed 3 extra calls to `headerListFromHeaders`
- Avoid `Object.keys` & `headerList` clone if `init.headers` is set
- Only empty `headersList` (`.splice`) if it's not already empty. 

## Benchmarks

**this patch**
```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark                    time (avg)        iter/s             (min … max)       p75       p99      p995
----------------------------------------------------------------------------- -----------------------------
Request without headers       1.86 µs/iter     536,440.7     (1.67 µs … 2.76 µs)   1.89 µs   2.76 µs   2.76 µs
Request with headers          1.96 µs/iter     509,440.5     (1.83 µs … 2.17 µs)   1.99 µs   2.17 µs   2.17 µs
```

**main**

```
cpu: 13th Gen Intel(R) Core(TM) i9-13900H
runtime: deno 1.36.1 (x86_64-unknown-linux-gnu)

benchmark                    time (avg)        iter/s             (min … max)       p75       p99      p995
----------------------------------------------------------------------------- -----------------------------
Request without headers       1.96 µs/iter     510,201.5     (1.81 µs … 2.64 µs)      2 µs   2.64 µs   2.64 µs
Request with headers          2.03 µs/iter     493,526.6     (1.84 µs … 2.31 µs)   2.08 µs   2.31 µs   2.31 µs
```

```js
Deno.bench("Request without headers", () => {
  const r = new Request("https://deno.land", {
    method: "POST",
    body: '{"foo": "bar"}',
  });
});

Deno.bench("Request with headers", () => {
  const r = new Request("https://deno.land", {
    method: "POST",
    body: '{"foo": "bar"}',
    headers: {
      "Content-Type": "application/json",
    },
  });
});
```
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