hyphenate

JavaScript performance comparison

Revision 2 of this test case created by Marshall Cline

Info

trying to see if a faster hyphenate function can be achieved

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var _uppercasePattern = /([A-Z])/g;
    var upperCaseLookaheadAssertion = /(?=[A-Z])/g;
    var str = 'ufihasdoiduhsfuhSdsaiudhsuhSfiudhuhJifdhnsiufGfhdnsuhifafOifuydhifuhsJfgsdfgg';
};
</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
regexp1
str.replace(_uppercasePattern, '-$1').toLowerCase();
pending…
regexp2
str.replace(_uppercasePattern, function(all, letter) {
  return '-' + letter.toLowerCase();
});
pending…
string manipulation
var arr = str.split('');
for (var i = 0, len = str.length; i < len; i++) {
  var letter = arr[i];
  if (letter >= 'A' && letter <= 'Z') {
    arr[i] = letter.toLowerCase();
    arr.splice(i, 0, '-');
  }
}
arr.join('');
pending…
using uncached lookahead assertion
str.replace(/(?=[A-Z])/g, '-').toLowerCase();
pending…
using cached lookahead assertion
str.replace(upperCaseLookaheadAssertion, '-').toLowerCase();
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