regex vs fori

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function order_regex(words){
    
    return words.split(' ').sort(function(a, b){
        return a.match(/\d/) - b.match(/\d/);
     }).join(' ');
  }   
  
  function order_fori(words){
    if (words == null || words.length == 0) return ""
    var comparator = {};
    words.split(" ").forEach(w => {
      for (var i = 0; i <= 9; i++) {
        if (w.indexOf(i + "") > -1) {
          comparator[w] = i;
          return
        }
      }
      comparator[w] = 9999
    })
    return words.split(" ").sort((a, b) => {
      return comparator[a] - comparator[b]
    }).join(" ")
  }
  
  var s = "asd4asfd asd3fadsf 2asdrewr df8sdaf 4 e4sr 6er e6wr 8ewr qw8er q5wer qwe4tr"

};
</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
regex
order_regex(s)
pending…
fori
order_fori(s)
pending…

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

0 Comments