replacing: regexp vs map

JavaScript performance comparison

Test case created by

Preparation code

<script>
  var text = "abcdeftest";
  
  function encode() {
      var b = "";
      for (var i = 0; i < text.length; i++) {
          b += String.fromCharCode(text.charCodeAt(i) - 1)
      }
      return b;
  }
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
regexp
text.replace(/./g, function(v) {
    return String.fromCharCode(v.charCodeAt() - 1);
});
pending…
map
String.fromCharCode.apply(null, text.split("").map(function(v) {
    return v.charCodeAt() - 1;
}));
pending…
for loop
encode(text);
pending…

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

Compare results of other browsers

0 Comments