switches (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
  obj = {
  
    '0': function(n) { return n + 0 } ,
    '1': function(n) { return n + 1 } ,
    '2': function(n) { return n + 2 } ,
    '3': function(n) { return n + 3 } ,
    '4': function(n) { return n + 4 } ,
    '5': function(n) { return n + 5 } ,
  }
  
  var res = 42
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
methods
i = Math.floor(Math.random() * 6)

res = obj[i](i)
ready
switch
i = Math.floor(Math.random() * 6)

switch(i) {

case 0: res = i
case 1: res = i + 1
case 2: res = i + 2
case 3: res = i + 3
case 4: res = i + 4
case 5: res = i + 5

}
ready

Revisions

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