Quote trim
JavaScript performance comparison
Info
This test attempts to establish which function that strips quotes around a word is the fastest.
Preparation code
<script>
Benchmark.prototype.setup = function() {
var myString = "'this is a test'",
result = "",
rx1 = /^'([^']+)'$/,
rx2B = /^'/,
rx2A = /'$/;
function trimQuote1(str) {
return str.replace(rx1, "$1");
}
function trimQuote2(str) {
return str.replace(rx2B, '').replace(rx2A, '');
}
function trimQuote3(str) {
if (!str.indexOf("'") && str.slice(-1) === "'") {
return str.slice(1, -1);
}
}
function trimQuote4(s) {
var str = s.match(rx1);
return str ? str[1] : '';
}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Regex 1 |
|
pending… |
Regex 2 |
|
pending… |
Slice replace |
|
pending… |
Match extract |
|
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. Here’s a list of current revisions for this page:
- Revision 1: published by Sam
- Revision 2: published by Sam
- Revision 3: published by Sam
0 comments