Object vs Regex 1 char
JavaScript performance comparison
Test case created
Preparation code
<script type="text/javascript">
var re = /^([.#])(.+)/,
dict = {
'.' : 'class',
'#' : 'id'
},
matches,
strA = '.isClass',
strB = '#isID';
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Testing in unknown unknown
| Test |
Ops/sec |
RegExp |
if (matches = strA.match(/^([.#])(.+)/)) matches[2]; // matches[1] is . or # else 'no match';
if (matches = strB.match(/^([.#])(.+)/)) matches[2]; // matches[1] is . or # else 'no match';
|
pending… |
Predefined Regexp |
if (matches = strA.match(re)) matches[2]; // matches[1] is . or # else 'no match';
if (matches = strB.match(re)) matches[2]; // matches[1] is . or # else 'no match';
|
pending… |
Object |
if (strA[0] in dict) strA.slice(1); // dict[strA[0]] has definition; else 'no match';
if (strB[0] in dict) strB.slice(1); // dict[strB[0]] has definition; else 'no match';
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
Compare results of other browsers
0 comments