Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nodeJs中处理if语句或使用return避免它们的速度更快的是什么?_Javascript_Node.js_Performance_If Statement - Fatal编程技术网

Javascript nodeJs中处理if语句或使用return避免它们的速度更快的是什么?

Javascript nodeJs中处理if语句或使用return避免它们的速度更快的是什么?,javascript,node.js,performance,if-statement,Javascript,Node.js,Performance,If Statement,在javascript或NodeJS中处理语句或使用returns避免它们,还有什么更快的方法 案例A 功能测试(x){ 如果(x3450),则为else{ 返回“output2” }否则{ 返回“output3” } } 案例B 功能测试(x){ 如果(x3450){ 返回“output2” } 返回“output3” } 我有一种直觉,案例B速度更快,性能更好,但我只想通过您的反馈确认一下。是的,我知道差别很小,这仅仅是一个微观优化,但是什么更快,案例a还是案例B?谢谢你们引用我不知道

在javascript或NodeJS中处理
语句或使用
return
s避免它们,还有什么更快的方法

案例A
功能测试(x){
如果(x<2000){
返回“output1”
}如果(x>3450),则为else{
返回“output2”
}否则{
返回“output3”
}
}
案例B
功能测试(x){
如果(x<2000){
返回“output1”
} 
如果(x>3450){
返回“output2”
} 
返回“output3”
}

我有一种直觉,案例B速度更快,性能更好,但我只想通过您的反馈确认一下。是的,我知道差别很小,这仅仅是一个微观优化,但是什么更快,案例a还是案例B?

谢谢你们引用我不知道的Javascript基准。嗯,我做了一些测试,与我的直觉相反,案例A稍微快一点,或者我可以说差异是可以忽略的,因为差异在误差范围内

这里的测试套件:

使用此代码

for (var i=0; i<10000; i++) {
  test(i)
}

for(var i=0;iif时始终隐含else(因为编译器应该知道在不满足条件时跳转到哪里。此“where”部分是隐含的else)。生成的编译代码中没有作用域。只有一个接一个的指令。其中一些指令被标记为可以作为跳转的目标

我很确定这两个代码示例的输出是完全相同的

        [check-equal] $x, 12142  // check equality
        [goto-if-zero] :label1   // Conditional jump to label1 position in case previous check was false
        [set-retval] 'output1'   // setting return value
        [goto] :end              // unconditional jump to the end of funtion
label1: [check-equal] $x, 798789 // check equality 
        [goto-if-zero] :label2   // Conditional jump to label2 position in case previous check was false
        [set-retval] 'output2'   // setting return value
        [goto] end               // unconditional jump to the end of funtion
label2: [setretval] 'output3'    // setting return value
end:    [return]                 // returing

差异将是如此之小以至于无关紧要……运行一个基准测试。无论如何,这是一个微观优化。可能运行一个性能测试?但如果编译的代码完全相同,我也不会感到惊讶。因此,这可以完全简化为
返回x!==798789
…如果你想知道什么更快:在你想要的代码上对它进行基准测试Javascript引擎。如果你想知道什么更具可读性:这是基于观点的。这些结果在舍入误差的范围内,基本上证明了这一点不重要。非常感谢,因此这只是一个美学问题
for (var i=0; i<10000; i++) {
  test(i)
}
        [check-equal] $x, 12142  // check equality
        [goto-if-zero] :label1   // Conditional jump to label1 position in case previous check was false
        [set-retval] 'output1'   // setting return value
        [goto] :end              // unconditional jump to the end of funtion
label1: [check-equal] $x, 798789 // check equality 
        [goto-if-zero] :label2   // Conditional jump to label2 position in case previous check was false
        [set-retval] 'output2'   // setting return value
        [goto] end               // unconditional jump to the end of funtion
label2: [setretval] 'output3'    // setting return value
end:    [return]                 // returing