admin管理员组文章数量:1405542
I am trying to understand how dispatch tables are an advantage over switch statements, let say we have the following two scenario to handle the same functionality
Switch statement
swtich(value){
case 'a':
fun1(value);
break;
case 'b':
fun2(value);
break;
case 'c':
fun3(value);
break;
}
Now the above switch statements can keep growing and can bee disorganized, hence there is an alternative with "dispatch tables" where we can makes use of an object
Dispatch Table
function dispatching(value){
var list = {
'a': fun1,
'b': fun2,
'c': fun3
}
return list[value]();
}
If i we are to follow the above function, wouldn't the Object list have to go through all its properties when invoked. I see how its makes it maintainable but does it improve in performance?
I am trying to understand how dispatch tables are an advantage over switch statements, let say we have the following two scenario to handle the same functionality
Switch statement
swtich(value){
case 'a':
fun1(value);
break;
case 'b':
fun2(value);
break;
case 'c':
fun3(value);
break;
}
Now the above switch statements can keep growing and can bee disorganized, hence there is an alternative with "dispatch tables" where we can makes use of an object
Dispatch Table
function dispatching(value){
var list = {
'a': fun1,
'b': fun2,
'c': fun3
}
return list[value]();
}
If i we are to follow the above function, wouldn't the Object list have to go through all its properties when invoked. I see how its makes it maintainable but does it improve in performance?
Share Improve this question asked Feb 23, 2016 at 6:43 RRPRRP 2,8639 gold badges32 silver badges55 bronze badges2 Answers
Reset to default 8...but does it improve in performance?
The odds of it mattering are extremely, extremely small. If it may matter for what you're doing, the best thing to do is code it both ways with representative options, and test it, for instance with http://jsperf..
But yes, looking up a property on an object is likely to be faster than the equivalent switch
. To process a switch
, the engine is required to test the cases in source-code order and stop at the first match; JavaScript switch
is really just another form of if...else if...
. (Of course, if the engine can see that the options are mutually-exclusive, it can optimize that process.) In contrast, modern JavaScript engines are just-in-time pilers and produce classes at runtime; property lookup by string name (list[value]
) isn't as fast as property lookup by name literal (list.a
), but it's still really fast, since the engine doesn't have to proceed sequentially and can look up the property using a balanced hash tree or similar.
Arguing the other way, however: If your functions are fairly short, the engine may be able to inline them in the switch
version (e.g., under the covers, move the function code into the switch
rather than actually making a function call), which could take it the other way in terms of performance. It depends a lot on what your functions are doing.
But again, the odds of it mattering are very low. This isn't going to be the bottleneck in your app. I'd make my decision based on which I felt was more maintainable in context, and worry about performance if and when I identified the dispatch mechanism being the chokepoint. (That's never happened for me yet. :-) )
There is a performance parison at http://jsperf./dispatch-table-vs-switch. They are very similar in terms of performance. I think using switch statements or dispatch tables is just based on coding preference.
One real world data point is the Facebook Reactjs library which tends to care about maintainability and performance, generally prefers switch statements in their action dispatchers.
本文标签: javascriptDispatch Tables vs Switch StatementsStack Overflow
版权声明:本文标题:javascript - Dispatch Tables vs Switch Statements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744290196a2599075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论