Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
use a class instead of redefining properties
For the context object passed into handlers, we were previously
redefining the getters every single time, which is slow in V8. This
change adds a class for contexts which holds those getters so they're
only defined once.
  • Loading branch information
bengl authored and talbenari1 committed Jul 27, 2019
1 parent 5afdc5b commit c5b5053
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions js/bootstrap/context.js
@@ -1,4 +1,7 @@
const { _route } = self._bindings;
const { _route, getPrivate } = self._bindings;
const urlSym = getPrivate('url');
const querySym = getPrivate('query');
const paramsSym = getPrivate('params');

const REGEX_CAPTURE_GROUPS = /\:([a-zA-Z0-9_]+)/g;
const REPLACE_CAPTURE_GROUPS = '(?<$1>[^\\/]+)'; // named capture groups
Expand Down Expand Up @@ -26,15 +29,21 @@ function parseParamsFromUrlPath(url) {
return groups;
}

class Context {
constructor(url) {
this[urlSym] = url;
}

get query() {
return this[paramsSym] || (this[paramsSym] = new URL(this[urlSym]).searchParams);
}

get params() {
return this[querySym] || (this[querySym] = parseParamsFromUrlPath(this[urlSym]));
}

}

export function generateContextObject(url) {
let params;
let query;
return {
get query() {
return params || (params = new URL(url).searchParams);
},
get params() {
return query || (query = parseParamsFromUrlPath(url));
}
};
return new Context(url);
}

0 comments on commit c5b5053

Please sign in to comment.