Cast To String

JavaScript performance comparison

Revision 8 of this test case created

Info

Fastest way to cast to a string.

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
String Function
var myVar = null,
  newString;
newString = String(myVar);
pending…
String Constructor
var myVar = null,
  newString;
newString = new String(myVar);
pending…
Concatenate Empty String
var myVar = null,
  newString;
newString = myVar + "";
pending…
toString Method
var myVar = null,
  newString;
newString = myVar.toString();
pending…
concat if truthy
var myVar = null,
  newString;
newString = myVar ? '' + myVar : '';
pending…
concat if truthy or 0
var myVar = null,
  newString;
newString = (myVar || myVar === 0) ? ('' + myVar) : '';
pending…
String func on truthy using ||
var myVar = null,
  newString;
newString = String(myVar || '');
pending…
concat if truthy using ||
var myVar = undefined,
  newString;
newString = (myVar || '') + '';
pending…
concat if truthy using &&
var myVar = undefined,
  newString;
newString = (myVar && '' + myVar) || '';
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment