Skip to content

Instantly share code, notes, and snippets.

@laverdet
Created February 28, 2011 00:03
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 laverdet/846719 to your computer and use it in GitHub Desktop.
Save laverdet/846719 to your computer and use it in GitHub Desktop.
String.prototype.method = function() {}
function func(str) {}
var stringObject = new String("hello world");
var stringPrimitive = "hello world";
exports.compare = {
"stringObject.method()": function() {
stringObject.method();
},
"stringPrimitive.method()": function() {
stringPrimitive.method();
},
"func(stringObject)": function() {
func(stringObject);
},
"func(stringPrimitive)": function() {
func(stringPrimitive);
},
};
require("bench").runMain();
benchmarking /Users/marcel/coercion.js
Please be patient.
Scores: (bigger is better)
stringObject.method()
Raw:
> 54363.63636363636
> 51750.24975024975
> 53206.793206793205
> 54864.13586413587
> 54911.08891108891
Average (mean) 53819.180819180816
func(stringPrimitive)
Raw:
> 38549.45054945055
> 44656.343656343655
> 44731.268731268734
> 44581.418581418584
> 43761.23876123876
Average (mean) 43255.944055944055
func(stringObject)
Raw:
> 39459.54045954046
> 43741.25874125874
> 44738.26173826174
> 43252.74725274725
> 44784.21578421578
Average (mean) 43195.20479520479
stringPrimitive.method()
Raw:
> 2061.9380619380618
> 2216.7832167832166
> 2217.7822177822177
> 2215.7842157842156
> 2212.787212787213
Average (mean) 2185.0149850149846
Winner: stringObject.method()
Compared with next highest (func(stringPrimitive)), it's:
19.63% faster
1.24 times as fast
0.09 order(s) of magnitude faster
Compared with the slowest (stringPrimitive.method()), it's:
95.94% faster
24.63 times as fast
1.39 order(s) of magnitude faster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment