toType optimizations
JavaScript performance comparison
Info
Trying to improve toType function from this article.
Preparation code
<script>
Benchmark.prototype.setup = function() {
var toString = Object.prototype.toString;
var toTypeOrig = function(obj) {
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
var toTypeOrigCompat = function(obj) {
return obj == null ? '' + obj : ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
var toTypeProto = function(obj) {
return toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
var toTypeProtoCompat = function(obj) {
return obj == null ? '' + obj : toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
toTypeOrig |
|
pending… |
toTypeOrigCompat |
|
pending… |
toTypeProto |
|
pending… |
toTypeProtoCompat |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments