regex vs slice (v2)

Revision 2 of this benchmark created by WebReflection 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 toTypeSI = function(obj) {
    var s = ({}).toString.call(obj);
    return s.slice(s.indexOf(" ") + 1, -1);
  };
  
  var noMatchCreated = function (obj) {
      return regExp.test(({}).toString.call(obj)) && RegExp.$1;
  };
</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
slice + indexOf
toTypeSI([1,2,3]);
ready
RegExp without match
noMatchCreated([1,2,3]);
ready

Revisions

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