Cast booleans

Benchmark created by Deerloper on


Setup

var t = 'true', T = 'TRUE', f = 'false', F = 'FALSE';
  
  var jsonize = function(s){
    return JSON.parse(s.toLowerCase());
  };
  String.prototype.bool = function() {
      return (/^true$/i).test(this.toLowerCase());
  };
  var proto = function(s){
    return s.bool();
  };
  var equalize = function(s){
    return (s.toLowerCase() === true);
  }
  Boolean.parse = function (str) {
    switch (str.toLowerCase ()) {
      case "true":
      case 1:
        return true;
      case "false":
      case 0:
        return false;
    }
  };
  var parser = function(s){
    return Boolean.parse(s);
  }
  var universal = function(s){
  var num = +s;
      return !isNaN(num) ? !!num : !!String(s).toLowerCase().replace(!!0,'');
  }
  
  var test = function(fu){
    fu(t); fu(T); fu(f); fu(F);
  };

Test runner

Ready to run.

Testing in
TestOps/sec
jsonize
test(jsonize);
ready
proto
test(proto);
ready
equalize
test(equalize);
ready
parser
test(parser);
ready
universal
test(universal);
ready

Revisions

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

  • Revision 1: published by Deerloper on