Quote trim

JavaScript performance comparison

Test case created by Sam

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);
      }
    }
};
</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
Regex 1
result = trimQuote1(myString);
pending…
Regex 2
result = trimQuote2(myString);
pending…
Slice replace
result = trimQuote3(myString);
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:

0 comments

Add a comment