url

Benchmark created on


Setup

function url_a(s) {
      // http://www.w3.org/TR/css3-values/#uris
      // Parentheses, commas, whitespace characters, single quotes (') and double
      // quotes (") appearing in a URI must be escaped with a backslash
      var s2 = s.replace(/(\(|\)|\,|\s|\'|\"|\\)/g, '\\$1');
      // WebKit has a bug when it comes to URLs that end with \
      // https://bugs.webkit.org/show_bug.cgi?id=28885
      if (/\\\\$/.test(s2)) {
        // Add a space to work around the WebKit bug.
        s2 += ' ';
      }
      return 'url("' + s2 + '")';
    }
    
    var re1 = /(\(|\)|\,|\s|\'|\"|\\)/g;
    var re2 = '\\$1';
    var re3 = /\\\\$/;
    
    function url_b(s) {
      // http://www.w3.org/TR/css3-values/#uris
      // Parentheses, commas, whitespace characters, single quotes (') and double
      // quotes (") appearing in a URI must be escaped with a backslash
      var s2 = s.replace(re1, re2);
      // WebKit has a bug when it comes to URLs that end with \
      // https://bugs.webkit.org/show_bug.cgi?id=28885
      if (re3.test(s2)) {
        // Add a space to work around the WebKit bug.
        s2 += ' ';
      }
      return 'url("' + s2 + '")';
    }
    
    function url_c(s) {
      // http://www.w3.org/TR/css3-values/#uris
      // Parentheses, commas, whitespace characters, single quotes (') and double
      // quotes (") appearing in a URI must be escaped with a backslash
      var s2 = s.replace(re1, '\\$1');
      // WebKit has a bug when it comes to URLs that end with \
      // https://bugs.webkit.org/show_bug.cgi?id=28885
      if (re3.test(s2)) {
        // Add a space to work around the WebKit bug.
        s2 += ' ';
      }
      return 'url("' + s2 + '")';
    }

Test runner

Ready to run.

Testing in
TestOps/sec
normal
url_a('http://www.w3.org/TR/css3-values/#uris');
ready
optimized
url_b('http://www.w3.org/TR/css3-values/#uris');
ready
optimized 2
url_c('http://www.w3.org/TR/css3-values/#uris');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.