regex vs slice

JavaScript performance comparison

Revision 24 of this test case created by James Jackson

Preparation code

<script>

  var normalTypeOfInAFunctionBaseline = function(obj) {
    return typeof obj
  }

  var normalTypeofWithArrayHandle = function(obj) {
    return Array.isArray(obj) ? "array" : typeof obj
  }

  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 toStringStore = ({}).toString;
  var toTypeRegExp_stored = function(obj) {
    return toStringStore.call(obj).match(/\s([a-z|A-Z]+)/)[1];
  }

  var toTypeCachedRegExp_stored = function(obj) {
    return toStringStore.call(obj).match(regExp)[1];
  }
  
  var toTypeSlice_stored = function(obj) {
    return toStringStore.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.call(obj).slice(8, -1);
  }

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

function* genStrip(basedOn) {
    var _itr= basedOn[Symbol.iterator]();
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    var _next= _itr.next();
    var _after= _itr.next();
    while(!_after.done) {
        yield _next.value;
        _next=_after;
        _after= _itr.next();
    }
    return _next.value;
}

var aSillyIdeaThatHasToBeSlow= function(obj) {
    var itr = genStrip(Object.prototype.toString.call(obj));
    var results = itr.next().value;
    for(var char of itr) {
        results += char;
    }
    return results
}

function* genStripWithToLower(basedOn) {
    var _itr= basedOn[Symbol.iterator]();
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    _itr.next()
    var _next= _itr.next();
    var _after= _itr.next();
    while(!_after.done) {
        yield _next.value.charCodeAt()|32;
        _next=_after;
        _after= _itr.next();
    }
    return _next.value.charCodeAt()|32;

}
var aSillyIdeaThatHasToBeSlowLower = function(obj){
    return String.fromCharCode(...genStripWithToLower(Object.prototype.toString.call(obj)));
}

var manualResultsHandle_backward = function(obj) {
    var result = Object.prototype.toString.call(obj)
    var len = result.length;
    var final = result[len-2]
    var i = len-3;
    for (i; i > 7; i--) {
        final = result[i] + final;
    }
    return final;
}
var manualResultsHandle = function(obj) {
    var result = Object.prototype.toString.call(obj)
    var len = result.length - 1;
    var final = result[8]
    var i = 9;
    for (i; i < len; i++) {
        final += result[i];
    }
    return final;
}
var manualResultsHandle_backward_lower = function(obj) {
    var result = Object.prototype.toString.call(obj)
    var len = result.length;
    var final = Array(len-8);
    var i = len-2;
    for (i; i > 7; i--) {
        final[i-8] = result.charCodeAt(i)|32;
    }
    return String.fromCharCode.apply(null, final);
}
var manualResultsHandle_lower = function(obj) {
    var result = Object.prototype.toString.call(obj)
    var len = result.length-1;
    var final = Array(len-8);
    var i = 8;
    for (i; i < len; i++) {
        final[i-8] = result.charCodeAt(i)|32;
    }
    return String.fromCharCode.apply(null, final);
}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  normalTypeOfInAFunctionBaseline
  normalTypeofWithArrayHandle
  toTypeRegExp
  toTypeCachedRegExp
  toTypeSlice
  toTypeRegExp_stored
  toTypeCachedRegExp_stored
  toTypeSlice_stored
  toTypeSliceObject
  toTypeSliceObjectLower
  toTypeSliceObject_stored
  toTypeSliceObjectLower_stored
  aSillyIdeaThatHasToBeSlow
  aSillyIdeaThatHasToBeSlowLower
  manualResultsHandle
  manualResultsHandle_backward
  manualResultsHandle_lower
  manualResultsHandle_backward_lower

};
</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
regex with cache, variable holding toString
toTypeCachedRegExp_stored([1,2,3])
pending…
Manually collecting the valid characters and making result lower case
manualResultsHandle_lower([1,2,3])
pending…
Manually collecting the valid characters
manualResultsHandle([1,2,3])
pending…
slice with global Object, variable holding toString
toTypeSliceObject_stored([1,2,3])
pending…
regex with cache
toTypeCachedRegExp([1,2,3]);
pending…
slice with global Object and toLowerCase
toTypeSliceObjectLower([1,2,3]);
pending…
Baseline, Function wrapped typeOf with array handling
normalTypeofWithArrayHandle([1,2,3])
pending…
Manually collecting the valid characters using a backwards for-loop
manualResultsHandle_backward([1,2,3])
pending…
slice with global Object
toTypeSliceObject([1,2,3]);
pending…
slice
toTypeSlice([1,2,3]);
pending…
slice, variable holding toString
toTypeSlice_stored([1,2,3])
pending…
Manually collecting the valid characters using a backwards for-loop and making result lower case
manualResultsHandle_backward_lower([1,2,3])
pending…
regex no cache, variable holding toString
toTypeRegExp_stored([1,2,3])
pending…
Going full over the top ES2015 - Gen functions, spreading, so on and to lower case
aSillyIdeaThatHasToBeSlowLower([1,2,3])
pending…
regex no cache
toTypeRegExp([1,2,3]);
pending…
Going full over the top ES2015 - Gen functions, spreading, so on
aSillyIdeaThatHasToBeSlow([1,2,3])
pending…
slice with global Object and toLowerCase, variable holding toString
toTypeSliceObjectLower_stored([1,2,3])
pending…
Baseline, Function wrapped typeOf
normalTypeOfInAFunctionBaseline([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