-
-
Save xk/861987 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// testing the effect of using an ES5 Getter | |
// to get the parsedUrl on an object | |
var url = require("url") | |
function AutoParseUrl (u) { | |
this.url = u | |
} | |
Object.defineProperty(AutoParseUrl.prototype, "parsedUrl", {get:function () { | |
return url.parse(this.url) | |
}, enumerable:true }) | |
function AutoParseCache (u) { | |
this.url = u | |
} | |
Object.defineProperty(AutoParseCache.prototype, "parsedUrl", {get:function () { | |
if (this._parsedUrl) return this._parsedUrl | |
return this._parsedUrl = url.parse(this.url) | |
}, enumerable:true }) | |
function AutoParseUrlQuery (u) { | |
this.url = u | |
} | |
Object.defineProperty(AutoParseUrlQuery.prototype, "parsedUrl", {get:function () { | |
return url.parse(this.url, true) | |
}, enumerable:true }) | |
function AutoParseCacheQuery (u) { | |
this.url = u | |
} | |
Object.defineProperty(AutoParseCacheQuery.prototype, "parsedUrl", {get:function () { | |
if (this._parsedUrl) return this._parsedUrl | |
return this._parsedUrl = url.parse(this.url, true) | |
}, enumerable:true }); | |
Object.defineProperty(AutoParseCacheQuery.prototype, 'selfShadowingGetter', { | |
get: function () { | |
var parsed= url.parse(this.url, true); | |
this.__proto__= null; // cut momentarily the prototype chain | |
this.selfShadowingGetter= parsed; // shadow the getter | |
this.__proto__= AutoParseCacheQuery.prototype; // restore the prototype chain | |
return parsed; | |
}, | |
enumerable:true | |
}); | |
function NoAutoParse (u) { | |
this.url = u | |
} | |
var u = "/path/to?something=foo#hash-bazoo" | |
exports.compare = | |
{ "selfShadowingGetter_NoCache" : function () { | |
var req = new AutoParseCacheQuery(u) | |
return [req.selfShadowingGetter, req.selfShadowingGetter, req.selfShadowingGetter, req.selfShadowingGetter] | |
} | |
, "selfShadowingGetter_Cache" : function () { | |
var req = new AutoParseCacheQuery(u); | |
var up= req.selfShadowingGetter; | |
return [up, up, up, up]; | |
} | |
, "auto" : function () { | |
var req = new AutoParseUrl(u) | |
return [req.parsedUrl, req.parsedUrl, req.parsedUrl, req.parsedUrl] | |
} | |
, "autoCache" : function () { | |
var req = new AutoParseCache(u) | |
return [req.parsedUrl, req.parsedUrl, req.parsedUrl, req.parsedUrl] | |
} | |
, "autoQuery" : function () { | |
var req = new AutoParseUrlQuery(u) | |
return [req.parsedUrl, req.parsedUrl, req.parsedUrl, req.parsedUrl] | |
} | |
, "autoCacheQuery" : function () { | |
var req = new AutoParseCacheQuery(u) | |
return [req.parsedUrl, req.parsedUrl, req.parsedUrl, req.parsedUrl] | |
} | |
, "noAuto" : function () { | |
var req = new NoAutoParse(u) | |
return [url.parse(req.url), url.parse(req.url), url.parse(req.url) | |
,url.parse(req.url)] | |
} | |
, "noAutoCache" : function () { | |
var req = new NoAutoParse(u) | |
, up = url.parse(req.url) | |
return [up, up, up, up] | |
} | |
, "noAutoCacheQuery" : function () { | |
var req = new NoAutoParse(u) | |
, up = url.parse(req.url, true) | |
return [up, up, up, up] | |
} | |
, "noAutoQuery" : function () { | |
var req = new NoAutoParse(u) | |
return [url.parse(req.url, true), url.parse(req.url, true) | |
,url.parse(req.url, true), url.parse(req.url, true)] | |
} | |
} | |
require("bench").runMain() | |
/* | |
$ node /Users/jorge/Desktop/shadowingGetter.js | |
benchmarking /Users/jorge/Desktop/shadowingGetter.js | |
Please be patient. | |
Scores: (bigger is better) | |
noAutoCache | |
Raw: | |
> 522.9540918163673 | |
> 503.4965034965035 | |
> 506.98602794411175 | |
> 521.9560878243512 | |
> 519.9600798403194 | |
Average (mean) 515.0705581843306 | |
autoCache | |
Raw: | |
> 281.71828171828173 | |
> 274.9003984063745 | |
> 271.9123505976096 | |
> 276.72327672327674 | |
> 279.72027972027973 | |
Average (mean) 276.99491743316446 | |
noAuto | |
Raw: | |
> 133.46613545816734 | |
> 129.74051896207584 | |
> 129.22465208747514 | |
> 136.45418326693226 | |
> 130.60817547357925 | |
Average (mean) 131.89873304964595 | |
auto | |
Raw: | |
> 103.8961038961039 | |
> 103.8961038961039 | |
> 104.16666666666667 | |
> 106.46766169154229 | |
> 102.2840119165839 | |
Average (mean) 104.14210961340014 | |
noAutoCacheQuery | |
Raw: | |
> 21.379980563654033 | |
> 22.33009708737864 | |
> 22.482893450635387 | |
> 22.395326192794546 | |
> 22.136669874879694 | |
Average (mean) 22.14499343386846 | |
autoCacheQuery | |
Raw: | |
> 20.408163265306122 | |
> 21.40077821011673 | |
> 21.215043394406944 | |
> 21.42161635832522 | |
> 21.42161635832522 | |
Average (mean) 21.17344351729605 | |
selfShadowingGetter_Cache | |
Raw: | |
> 20.0381679389313 | |
> 20.192307692307693 | |
> 20.211742059672762 | |
> 20.23121387283237 | |
> 20.669291338582678 | |
Average (mean) 20.26854458046536 | |
selfShadowingGetter_NoCache | |
Raw: | |
> 19.41747572815534 | |
> 19.398642095053347 | |
> 19.305019305019304 | |
> 19.398642095053347 | |
> 19.49317738791423 | |
Average (mean) 19.40259132223911 | |
noAutoQuery | |
Raw: | |
> 5.405405405405405 | |
> 5.571030640668524 | |
> 5.58659217877095 | |
> 5.612722170252573 | |
> 5.591798695246971 | |
Average (mean) 5.553509818068885 | |
autoQuery | |
Raw: | |
> 5.29567519858782 | |
> 5.504587155963303 | |
> 5.46448087431694 | |
> 5.509641873278237 | |
> 5.560704355885079 | |
Average (mean) 5.467017891606276 | |
Winner: noAutoCache | |
Compared with next highest (autoCache), it's: | |
46.22% faster | |
1.86 times as fast | |
0.27 order(s) of magnitude faster | |
Compared with the slowest (autoQuery), it's: | |
98.94% faster | |
94.21 times as fast | |
1.97 order(s) of magnitude faster | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment