typeof

Benchmark created by Llorx on


Setup

var mockdata = [];
  
  function notToJson(val) {
      return val !== val || val === Infinity || val === -Infinity;
  }
  
  for (var i = 0; i < 1000; i++) {
      var n;
      if (Math.random()>0.75) {
          n = Math.floor(Math.random()*10000);
          if (Math.random()>0.5) {
              n = n.toString();
          }
      } else {
          if (Math.random()>0.5) {
              n = NaN;
          } else {
              if (Math.random()>0.5) {
                  n = Infinity;
              } else {
                  n = -Infinity;
              }
          }
      }
      mockdata.push(n);
  }

Test runner

Ready to run.

Testing in
TestOps/sec
typeof
for (var i = 0; i < 1000; i++) {
    var res = typeof mockdata[i] === "string";
}
ready
notToJson
for (var i = 0; i < 1000; i++) {
    var res = notToJson(mockdata[i]);
}
ready
toString
for (var i = 0; i < 1000; i++) {
    var res = mockdata[i].toString();
}
ready
JSON.stringify
for (var i = 0; i < 1000; i++) {
    var res = JSON.stringify(mockdata[i]);
}
ready
Full typeof
for (var i = 0; i < 1000; i++) {
    var res = typeof mockdata[i]==="string"?JSON.stringify(mockdata[i]):mockdata[i].toString();
}
ready
Full notToJson
for (var i = 0; i < 1000; i++) {
    var res = notToJson(mockdata[i])?mockdata[i].toString():JSON.stringify(mockdata[i]);
}
ready
deleted
var deleted = "deleted"; // How to delete a snippet???
ready

Revisions

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

  • Revision 1: published by Llorx on