string regexp replace vs loop indexof replaces (v13)

Revision 13 of this benchmark created on


Preparation HTML

<script>
  var test_str = '1.2.3.4.5.6.7.8.9.0';
  var r = /\./g;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
string replace regexp
test_str.replace(/\./g, ',');
ready
loop indexof
while (test_str.indexOf('.') !== -1) {
   test_str = test_str.replace('.', ',');
}
ready
precompiled regexp
test_str.replace(r, ',');
ready
simple loop
for(var i=0; i< test_str.length; i++) if(test_str[i]===".") test_str[i]=","
ready
enhanced while
var lastLocation;
while ((lastLocation = test_str.indexOf('.', lastLocation)) !== -1) {
   test_str = test_str.replace('.', ',');
}
ready
non-regex replace with g flag
test_str.replace('.', ',', 'g');
ready

Revisions

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