regex vs slice (v5)

Revision 5 of this benchmark created by nfeldman on


Preparation HTML

<script>
  var toTypeRegExp = function(obj) {
    return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1];
  }
  
  var regExp = /\s([a-z|A-Z]+)/;
  
  var toTypeCachedRegExp = function(obj) {
    return ({}).toString.call(obj).match(regExp)[1];
  }
  
  
  var toTypeSlice = function(obj) {
    return ({}).toString.call(obj).slice(8, -1);
  }
  
  var toTypeSplitSlice = function(obj) {
    return ({}).toString.call(obj).split(" ").slice(0,-1);
  }
  
  var toTypeSubstring = (function () {
    var toStr = Object.prototype.toString;
     return function (obj) {
        var t = toStr.call(obj);
       return t.substring(8, t.length - 1).toLowerCase();
     }
  }());
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
regex no cache
toTypeRegExp([1,2,3]);
 
ready
regex with cache
toTypeCachedRegExp([1,2,3]);
ready
slice
toTypeSlice([1,2,3]);
ready
splitslice
toTypeSplitSlice([1,2,3]);
ready
toTypeSubstring
toTypeSubstring([1,2,3]);
ready

Revisions

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