Double Space to Single Space

JavaScript performance comparison

Revision 2 of this test case created

Info

Testing Single Spaces to Spaces - doubt it even matters - but we'll see...

Using underscoreJS cuz i'm lazy like that.

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.string/2.3.1/underscore.string.min.js"></script>



<script>
  var str = ' abcdefghijklmnopqrstuvwxyz',
  output = "",
  spaces = function() { return _.str.pad("", Math.floor(Math.random()*5)); };
  for (i = 0; i < 1000; i++) {
    output += str.charAt(i) + spaces();
  }
</script>

Preparation code output

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
var newStr = str.trim().replace(/\s+/g, " ");
pending…
regex 2
var newStr = str.trim().replace(/ +/g, " ");
pending…
regex 3
var newStr = str.trim().replace(/\s\s+/g, " ");
pending…
regex 4
var newStr = str.trim().replace(/  +/g, " ");
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