Skip to content

perf: cache defineGetter #6516

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ defineGetter(req, 'query', function query(){
var querystring = parse(this).query;

return queryparse(querystring);
});
}, true);

/**
* Check if the incoming request contains the "Content-Type"
Expand Down Expand Up @@ -299,7 +299,7 @@ defineGetter(req, 'protocol', function protocol(){
return index !== -1
? header.substring(0, index).trim()
: header.trim()
});
}, true);

/**
* Short-hand for:
Expand All @@ -312,7 +312,7 @@ defineGetter(req, 'protocol', function protocol(){

defineGetter(req, 'secure', function secure(){
return this.protocol === 'https';
});
}, true);

/**
* Return the remote address from the trusted proxy.
Expand All @@ -327,7 +327,7 @@ defineGetter(req, 'secure', function secure(){
defineGetter(req, 'ip', function ip(){
var trust = this.app.get('trust proxy fn');
return proxyaddr(this, trust);
});
}, true);

/**
* When "trust proxy" is set, trusted proxy addresses + client.
Expand All @@ -350,7 +350,7 @@ defineGetter(req, 'ips', function ips() {
addrs.reverse().pop()

return addrs
});
}, true);

/**
* Return subdomains as an array.
Expand Down Expand Up @@ -378,7 +378,7 @@ defineGetter(req, 'subdomains', function subdomains() {
: [hostname];

return subdomains.slice(offset);
});
}, true);

/**
* Short-hand for `url.parse(req.url).pathname`.
Expand All @@ -389,7 +389,7 @@ defineGetter(req, 'subdomains', function subdomains() {

defineGetter(req, 'path', function path() {
return parse(this).pathname;
});
}, true);

/**
* Parse the "Host" header field to a host.
Expand All @@ -415,7 +415,7 @@ defineGetter(req, 'host', function host(){
}

return val || undefined;
});
}, true);

/**
* Parse the "Host" header field to a hostname.
Expand All @@ -442,7 +442,7 @@ defineGetter(req, 'hostname', function hostname(){
return index !== -1
? host.substring(0, index)
: host;
});
}, true);

/**
* Check if the request is fresh, aka
Expand Down Expand Up @@ -470,7 +470,7 @@ defineGetter(req, 'fresh', function(){
}

return false;
});
}, true);

/**
* Check if the request is stale, aka
Expand All @@ -483,7 +483,7 @@ defineGetter(req, 'fresh', function(){

defineGetter(req, 'stale', function stale(){
return !this.fresh;
});
}, true);

/**
* Check if the request was an _XMLHttpRequest_.
Expand All @@ -495,20 +495,34 @@ defineGetter(req, 'stale', function stale(){
defineGetter(req, 'xhr', function xhr(){
var val = this.get('X-Requested-With') || '';
return val.toLowerCase() === 'xmlhttprequest';
});
}, true);

/**
* Helper function for creating a getter on an object.
*
* @param {Object} obj
* @param {String} name
* @param {Function} getter
* @param {Object} obj - The object to define the property on.
* @param {String} name - The name of the property.
* @param {Function} getter - The function to call to get the value.
* @param {boolean} enableCache - Whether to cache the value.
* @private
*/
function defineGetter(obj, name, getter) {
function defineGetter(obj, name, getter, enableCache) {
let cache;
let cacheValid = false;

Object.defineProperty(obj, name, {
configurable: true,
enumerable: true,
get: getter
get: function () {
if (!enableCache || !cacheValid) {
cache = getter.call(this);
cacheValid = true;
}
return cache;
},
set: function (value) {
cache = value;
cacheValid = true;
}
});
}