condition-vs-regex-vs-every-loop-vs-indexOf

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = 1,
        b = 2,
        c = 3,
        d = 4,
        e = 5;
   
    function isTrue(vars, vals) {
      return vals.every(function(val, i) {
        return vars[i] == val
      });
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
condional
if (a == 1 && b == 2 && c == 3 && d == 4 && e == 5) {
  console.log('hey');
}
pending…
every
if (isTrue([a, b, c, d, e], [1, 2, 3, 4, 5])) {
  console.log('hey');
}
pending…
indexOf
if (['a', 'b', 'c'].indexOf(val) !== -1) {
  console.log('hey');
}
pending…
indexOf (not litteral)
var tab = ['a', 'b', 'c'];
if (tab.indexOf(val) !== -1) {
  console.log('hey');
}
pending…
RegExp (not litteral)
var rg = new RegExp('^a|b|c$');
if (rg.test(val)) {
  console.log('hey');
}
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment