Skip to content

Instantly share code, notes, and snippets.

@bahamas10
Created September 21, 2012 19:47
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 bahamas10/3763490 to your computer and use it in GitHub Desktop.
Save bahamas10/3763490 to your computer and use it in GitHub Desktop.
string casting speed in node.js/javascript
var a = 5,
count = 100 * 1000 * 1000;
for (var i = 0; i < count; i++)
''+a;
var a = 5,
count = 100 * 1000 * 1000;
for (var i = 0; i < count; i++)
String(a);
var a = 5,
count = 100 * 1000 * 1000;
for (var i = 0; i < count; i++)
a.toString();

String() constructor cast

dave @ [ bahamas10 :: (Darwin) ] ~/temp/benchmark $ time node String.js 

real	0m2.618s
user	0m2.613s
sys	0m0.017s

toString() cast

dave @ [ bahamas10 :: (Darwin) ] ~/temp/benchmark $ time node toString.js 

real	0m1.323s
user	0m1.315s
sys	0m0.013s

''+num cast (coercion)

dave @ [ bahamas10 :: (Darwin) ] ~/temp/benchmark $ time node coerce.js 

real	0m0.690s
user	0m0.681s
sys	0m0.011s

Coercing wins!

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