Double Space to Single Space

JavaScript performance comparison

Test case created by Jonathon Hibbard


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=""></script>
<script src=""></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();

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

Compare results of other browsers


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:

1 comment

Jonathon Hibbard (revision owner) commented :

And, I have to say I am surprised.

Comment form temporarily disabled.

Add a comment