_.isString vs typeof string

JavaScript performance comparison

Revision 7 of this test case created

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var teststring = "Hallo! Ich bin ein String!";
    var testobj = { not: "a string" };
   
    var typeOfCheck = {
      isString: function(obj) {
        return typeof(teststring) === "string";
      }
    };
   
    var objectNameCheck = {
      isString: function(obj) {
        return Object.prototype.toString.call(obj) == '[object String]';
      }
    };
   
    var duckTypeCheck = {
      isString: function(obj) {
        return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
      }
    };
   
   
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
typeof String
typeof(teststring) === "string";
typeof(testobj) === "string";
pending…
Underscore isString
_.isString(teststring);
_.isString(testobj);
pending…
typeOfCheck
typeOfCheck.isString(teststring);
typeOfCheck.isString(testobj);
pending…
duckTypeCheck
duckTypeCheck.isString(teststring);
duckTypeCheck.isString(testobj);
pending…
objectNameCheck
objectNameCheck.isString(teststring);
objectNameCheck.isString(testobj);
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