regex vs slice (v6)

Revision 6 of this benchmark created by PhiLho on


Description

Regex can be simplified...
Removed toTypeSubstring which mixes two different optimizations.

Preparation HTML

<script>
  var toTypeRegExp = function(obj) {
    return ({}).toString.call(obj).match(/\s(\w+)/)[1];
  }
  
  var regExp = /\s(\w+)/;
  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);
  }
  
</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

Revisions

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