.replace() vs. .split().join() vs .replaceAll() (v30)

Revision 30 of this benchmark created on


Setup

var mystring = 'okay.this.is.a.string';
  var dynamicRegex = new RegExp("\.", "gm");
  var staticRegex = /\./g;

Teardown



            console.log(result);
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
.replace(/\./g,' ')
result = mystring.replace(/\./g, '_');
ready
.replace(new RegExp(".","gm")," ")
result = mystring.replace(new RegExp("\.", "gm"), "_");
ready
.split('.').join(' ')
result = mystring.split('.').join('_');
ready
Stored Dynamic Regex
result = mystring.replace(dynamicRegex, "_");
ready
Stored static Regex
result = mystring.replace(staticRegex, "_");
ready
String replace
result = mystring.replace('.', '_');
ready
For-Loop-Self-Cutter
for (
  var i = 0, l = mystring.length, result = "";
  i < l;
  (result += (mystring[i] == "." ? "_" : mystring[i])) && i++
);
ready

Revisions

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