Skip to content

Instantly share code, notes, and snippets.

@visnup
Created February 27, 2011 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save visnup/845827 to your computer and use it in GitHub Desktop.
Save visnup/845827 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
// extending string and calling functions on it sucks
function a() { }
String.prototype.a = a;
var str = ""
, strObj = new String(str);
Array.prototype.a = a;
var arr = [];
RegExp.prototype.a = a;
var rgx = /a/;
a.prototype.a = a;
var obj = new a();
exports.compare = {
"plain": function() {
a();
},
"string (literal) prototype": function() {
str.a();
},
"string (coerced literal) prototype": function() {
String(str).a();
},
"string (object) prototype": function() {
strObj.a();
},
"array prototype": function() {
arr.a();
},
"regex prototype": function() {
rgx.a();
},
"object prototype": function() {
obj.a();
}
};
require("bench").runMain();
/*
$ node plain-function-vs-prototyped-function.js
benchmarking plain-function-vs-prototyped-function.js
Please be patient.
Scores: (bigger is better)
plain
Raw:
> 46578.42157842158
> 45553.446553446556
> 47217.782217782216
> 42516.48351648352
> 47426.57342657343
Average (mean) 45858.541458541455
object prototype
Raw:
> 40020.97902097902
> 42557.442557442555
> 39831.168831168834
> 43007.992007992005
> 42729.27072927073
Average (mean) 41629.370629370635
string (object) prototype
Raw:
> 40307.692307692305
> 42668.331668331666
> 43196.803196803194
> 41781.218781218784
> 39830.16983016983
Average (mean) 41556.843156843155
array prototype
Raw:
> 39521.47852147852
> 41196.803196803194
> 40234.76523476523
> 42425.57442557443
> 41656.343656343655
Average (mean) 41006.993006993005
regex prototype
Raw:
> 39952.04795204795
> 33506.493506493505
> 41236.76323676324
> 41387.61238761239
> 39808.191808191805
Average (mean) 39178.22177822177
string (literal) prototype
Raw:
> 335.99202392821536
> 1282.7172827172826
> 1296.7032967032967
> 1473.5264735264736
> 365.1741293532338
Average (mean) 950.8226412457004
string (coerced literal) prototype
Raw:
> 1304.6953046953047
> 1238.7612387612387
> 272.72727272727275
> 332.00398803589235
> 1192.807192807193
Average (mean) 868.1989994053802
Winner: plain
Compared with next highest (object prototype), it's:
9.22% faster
1.1 times as fast
0.04 order(s) of magnitude faster
Compared with the slowest (string (coerced literal) prototype), it's:
98.11% faster
52.82 times as fast
1.72 order(s) of magnitude faster
*/
@huned
Copy link

huned commented Feb 27, 2011

Whoah. That is a f'huge difference.

@visnup
Copy link
Author

visnup commented Feb 27, 2011

yeah, it's kinda scary.

@visnup
Copy link
Author

visnup commented Feb 27, 2011

updated with the results of a plain new object, which is totally fine. not sure why extending strings sucks so much.

@visnup
Copy link
Author

visnup commented Feb 27, 2011

threw in extending Array and RegExp, also no sweat. still just String, sitting there peeing in the pool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment