我应该在没有性能问题的情况下在 JavaScript 中使用大的 switch 语句吗? [英] Should I use big switch statements in JavaScript without performance problems?

查看:26
本文介绍了我应该在没有性能问题的情况下在 JavaScript 中使用大的 switch 语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用谷歌搜索了一下,但我仍然感到迷茫.我正在用 JavaScript 编写一个 CPU 模拟器(目前在我的例子中是 Z80).它的内心有一个巨大的switch语句.虽然我当然可以运行一些基准测试,但我不能确定未来不同浏览器的 JavaScript 引擎(好吧,没有人可以,我知道,但这里应该有人比我更了解不同浏览器的 JavaScript 引擎......).

I've google'd a bit but I still feel to be lost. I am writing a CPU emulator in JavaScript (namely Z80 in me case, currently). There is a huge switch statement in its heart. Though I can run some benchmarks of course, I can't be sure about future JavaScript engines of different browsers (ok, nobody can be, I know, but somebody should have greater knowledge of javascript engines of different browsers here than me ...).

使用其他结构比使用 switch 语句更快吗?像函数数组等.还有类似的问题:我有可能有前缀 Z80 操作码的情况.开关的一些case"分支需要内部的另一个开关,但不是全部.我正在考虑将整个内容转换为更大(但只有一级深")的开关构造,以便正常情况下在 0-255 之间具有操作码,256-511 作为 0xDD 前缀等,其中一些会有更多的案例"(没有中断)对于单个相同的代码.或者,即使对于基本操作码表(256 个case"分支),我也应该尽可能避免使用大的 switch 语句?

Is it faster to use other constructs than switch statement? Like an array of functions, etc. Also similar problem: I have the situation to have possible prefixed Z80 opcodes. Some 'case' branches of the switch needs another switch inside, but not all. I am thinking to convert the whole stuff into an even bigger (but only one level "deep") switch construct to have opcodes between 0-255 as normal, 256-511 as 0xDD prefixed etc, some of them would have more 'case's (without break) for the single same code. Or should I avoid big switch statements as much as possible even for the base opcode table (256 'case' branches)?

对于 C 代码,编译器会在这种情况下创建一个跳转表,这是相当快的.但是 javascript 呢?

For C code, compiler would create a jump table in this situation which is quite fast. But what's about javascript?

另一个类似的问题:是否值得拥有一个只有两个case"分支和一个default"条目的 switch 语句,或者真的是 if ... else 更好的情况?

Another similar question: is it worth to have a switch statement with only two 'case' branches and a 'default' entry, or it's really the situation where if ... else is better?

声明我在我的 switch 结构中只使用数字(和整数 - 尽管它在 JavaScript 中意义不大,因为 AFAIK 它只有一种数字类型)值可能很重要.

It can be important to declare that I use only numeric (and integer - though it does not mean too much in JavaScript since AFAIK it only has one numeric type) values in my switch constructs.

推荐答案

switch 至少和必须评估许多 if 一样快>s 和 elses.即使不了解 JavaScript 引擎和优化器的内部结构,我们也肯定知道这一点.

The switch is at least as fast as having to evaluate many ifs and elses. Even without knowing the insides of JavaScript engines and optimizers, this we know for sure.

对于像您这样的解释器,最好将函数存储在字典(数组)中,然后在该数组中对代码进行二进制查找.

In case of an interpreter such as your case, it might be even better to store functions in a dictionary (array) instead, and do a binary lookup of your code in that array.

因此,创建一个数组,其中包含您将放在案例中的代码作为索引和 JavaScript 代码(函数)作为值,然后执行您在数组中找到的代码.

So create an array with the code you would put in the cases as index and JavaScript code (functions) as values, and just execute the code you find at the array.

这篇关于我应该在没有性能问题的情况下在 JavaScript 中使用大的 switch 语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆