Test name | Executions per second |
---|---|
Switch | 9382.8 Ops/sec |
Object Function | 9616.7 Ops/sec |
Object Property | 9146.4 Ops/sec |
var str = 'abc';
str = str.charAt(Math.floor(Math.random() * 3));
function test (str) {
switch (str) {
case 'a': return 'A'; break;
case 'b': return 'B'; break;
case 'c': return 'C'; break;
}
}
console.log(test(str));
const obj = {
a: () => 'A',
b: () => 'B',
c: () => 'C'
};
console.log(obj[str]());
const obj = {
a: 'A',
b: 'B',
c: 'C'
};
console.log(obj[str]);