regex vs slice (v22)

Revision 22 of this benchmark created by James Jackson 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 toTypeSliceObject = function(obj) {
    return Object.prototype.toString.call(obj).slice(8, -1);
  }

  var toTypeSliceObjectLower = function(obj) {
    return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
  }

  var toStringCall = Object.prototype.toString;

  var toTypeSliceObject_stored = function(obj) {
    return toStringCall(obj).slice(8, -1);
  }

  var toTypeSliceObjectLower_stored = function(obj) {
    return toStringCall(obj).slice(8, -1).toLowerCase();
  }
  
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
slice with global Object
toTypeSliceObject([1,2,3]);
ready
slice with global Object and toLowerCase
toTypeSliceObjectLower([1,2,3]);
ready
regex no cache
toTypeRegExp([1,2,3]);
ready
slice
toTypeSlice([1,2,3]);
ready
regex with cache
toTypeCachedRegExp([1,2,3]);
ready

Revisions

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