Cast To String (v20)

Revision 20 of this benchmark created on


Setup

function numberToString(n) {
    if (n === 0) 
      return '0';
    var arr = [];
    while (n) {
      arr.push(n % 10 + 48);
      n = (n / 10)|0;
    }
    return String.fromCharCode.apply(null, arr.reverse());
  }
  
  function numberToString2(n) {
    if (n === 0) 
      return '0';
    var arr = [];
    while (n) {
      arr.unshift(n % 10 + 48);
      n = (n / 10)|0;
    }
    return String.fromCharCode.apply(null, arr);
  }

Test runner

Ready to run.

Testing in
TestOps/sec
String Function
var myVar = 8852470024,
    newString;
newString = String(myVar);
ready
String Constructor
var myVar = 8852470024,
    newString;
newString = new String(myVar);
ready
Concatenate Empty String
var myVar = 8852470024,
    newString;
newString = myVar + "";
ready
toString Method
var myVar = 8852470024,
    newString;
newString = myVar.toString();
ready
Own casting to string function
var myVar = 8852470024,
    newString;
newString = numberToString(myVar);
ready
Own casting to string function 2
var myVar = 8852470024,
    newString;
newString = numberToString2(myVar);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.