regex vs slice

JavaScript performance comparison

Revision 21 of this test case created by James Jackson

Preparation code

<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.call;

  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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
pre-store call 1
toTypeSliceObject_stored([1,2,3])
pending…
slice with global Object
toTypeSliceObject([1,2,3]);
pending…
slice
toTypeSlice([1,2,3]);
pending…
slice with global Object and toLowerCase
toTypeSliceObjectLower([1,2,3]);
pending…
regex no cache
toTypeRegExp([1,2,3]);
pending…
pre-store call 2
toTypeSliceObjectLower_stored([1,2,3])
pending…
regex with cache
toTypeCachedRegExp([1,2,3]);
pending…

Compare results of other browsers

Revisions

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

0 Comments