Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
use private fields intead of defineProperty
`node-fetch` is implemented this way as well. This adds about 5% to RPS.
  • Loading branch information
bengl authored and tlhunter committed Jul 17, 2019
1 parent 9d16739 commit 67037b4
Showing 1 changed file with 69 additions and 29 deletions.
98 changes: 69 additions & 29 deletions js/bootstrap/index.js
Expand Up @@ -454,15 +454,6 @@
}
self.fetch = fetch;

function defineReadOnly(obj, prop, value) {
Reflect.defineProperty(obj, prop, {
value,
enumerable: true,
configurable: true,
writable: false
});
}

function chunkAsArrayBuffer(chunk) {
if (!(chunk instanceof ArrayBuffer)) {
if (typeof chunk === 'string') {
Expand Down Expand Up @@ -689,55 +680,104 @@
}

class Response {
#rawHeaders; // not yet instantiated
#headers; // instantiated
#status;
#statusText;
#body;
#_bodyString;
constructor(body, init = {}) {
defineReadOnly(this, 'status', init.status || 200);
defineReadOnly(this, 'statusText', init.statusText || 'OK');
defineReadOnly(
this,
'headers',
init.headers instanceof Headers
? init.headers
: new Headers(init.headers)
);
this.#status = init.status || 200;
this.#statusText = init.statusText || 'OK';
if (!(init.headers instanceof Headers)) {
this.#rawHeaders = init.headers;
} else {
this.#headers = init.headers;
}
if (body instanceof ReadableStream || body instanceof TransformStream) {
defineReadOnly(this, 'body', body);
this.#body = body;
} else if (typeof body === 'string') {
defineReadOnly(this, '_bodyString', body);
this.#_bodyString = body;
} else if (isBufferish(body)) {
defineReadOnly(this, 'body', new StringReadable(body));
this.#body = new StringReadable(body);
}
}

get headers() {
if (!this.#headers) {
this.#headers = new Headers(this.#rawHeaders);
}
return this.#headers;
}

get status() {
return this.#status;
}

get statusText() {
return this.#statusText;
}

get body() {
return this.#body;
}

get _bodyString() {
return this.#_bodyString;
}

}
BodyMixin.mixin(Response);

class Request {
#preHeaders; // not yet instantiated
#rawHeaders; // not yet instantiated
#headers; // instantiated
#url;
#method;
#body;
#_bodyString;
constructor(input, init = {}) {
// TODO support `input` being a Request
defineReadOnly(this, 'url', input);
this.#url = input;

if (!(init.headers instanceof Headers)) {
this.#preHeaders = init.headers;
this.#rawHeaders = init.headers;
} else {
this.#headers = init.headers;
}
defineReadOnly(this, 'method', init.method || 'GET');
this.#method = init.method || 'GET';

if (init.body instanceof ReadableStream || init.body instanceof self.FormData) {
defineReadOnly(this, 'body', init.body);
this.#body = init.body;
} else if (typeof init.body === 'string') {
defineReadOnly(this, '_bodyString', init.body);
this.#_bodyString = init.body;
} else if (isBufferish(init.body)) {
defineReadOnly(this, 'body', new StringReadable(init.body));
this.#body = new StringReadable(init.body);
}
}

get headers() {
if (!this.#headers) {
this.#headers = new Headers(this.#preHeaders);
this.#headers = new Headers(this.#rawHeaders);
}
return this.#headers;
}

get url() {
return this.#url;
}

get method() {
return this.#method;
}

get body() {
return this.#body;
}

get _bodyString() {
return this.#_bodyString;
}
}
BodyMixin.mixin(Request);

Expand Down

0 comments on commit 67037b4

Please sign in to comment.