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: use class instead of object literals with getters #3138

Merged
merged 12 commits into from
Apr 21, 2024
8 changes: 8 additions & 0 deletions benchmarks/fetch/request-creation.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { bench, run } from 'mitata'
import { Request } from '../../lib/web/fetch/request.js'

const input = 'https://example.com/post'

bench('new Request(input)', () => new Request(input, undefined))

await run()
20 changes: 13 additions & 7 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {

let patchMethodWarning = false

class RequestRealmSettingsObject {
baseUrl = getGlobalOrigin()

get origin () {
return this.baseUrl?.origin
}

policyContainer = makePolicyContainer()
}

// https://fetch.spec.whatwg.org/#request-class
class Request {
// https://fetch.spec.whatwg.org/#dom-request
Expand All @@ -56,13 +66,9 @@ class Request {

// https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
this[kRealm] = {
settingsObject: {
baseUrl: getGlobalOrigin(),
get origin () {
return this.baseUrl?.origin
},
policyContainer: makePolicyContainer()
}
// Note: Slow initialization of object literals with getters.
// TODO: Remove workaround.
settingsObject: new RequestRealmSettingsObject()
}

// 1. Let request be null.
Expand Down
Loading