Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.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 带指定变量的条件与内联条件检查_Javascript_Variables_Conditional Statements_Variable Assignment - Fatal编程技术网

Javascript 带指定变量的条件与内联条件检查

Javascript 带指定变量的条件与内联条件检查,javascript,variables,conditional-statements,variable-assignment,Javascript,Variables,Conditional Statements,Variable Assignment,无法为这个问题找到更好的标题,因此编辑建议将不胜感激 我想知道在使用指定变量检查条件和内联条件之间是否有区别。 例如: 选项1: // inline conditions check function isSomething(){ return (1 > 2 || 'a' == 'a' || 2 < 4) || (55 == 1 || (32 > 4 || 'a' == 'a') && 6 > 2) ||

无法为这个问题找到更好的标题,因此编辑建议将不胜感激

我想知道在使用指定变量检查条件和内联条件之间是否有区别。
例如:

选项1:

// inline conditions check
function isSomething(){
    return (1 > 2 || 'a' == 'a' || 2 < 4) || 
           (55 == 1 || (32 > 4 || 'a' == 'a') && 6 > 2) || 
           ('abc' != 'bca' && 3 == 3);
}
// pre assigned variables condition check
function isSomething(){
    const conditionA = 1 > 2 || 'a' == 'a' || 2 < 4; // some complex condition
    const conditionB = 55 == 1 || (32 > 4 || 'a' == 'a') && 6 > 2; // some complex condition
    const conditionC = 'abc' != 'bca' && 3 == 3 // some complex condition

    const result = conditionA || conditionB || conditionC;
    return result;
}
//内联条件检查
函数isSomething(){
返回(1>2 | | a'='a'| | 2<4)|
(55==1 | | |(32>4 | | |‘a’==a’&&6>2)|
(“abc”!=“bca”&&3==3);
}
选项2:

// inline conditions check
function isSomething(){
    return (1 > 2 || 'a' == 'a' || 2 < 4) || 
           (55 == 1 || (32 > 4 || 'a' == 'a') && 6 > 2) || 
           ('abc' != 'bca' && 3 == 3);
}
// pre assigned variables condition check
function isSomething(){
    const conditionA = 1 > 2 || 'a' == 'a' || 2 < 4; // some complex condition
    const conditionB = 55 == 1 || (32 > 4 || 'a' == 'a') && 6 > 2; // some complex condition
    const conditionC = 'abc' != 'bca' && 3 == 3 // some complex condition

    const result = conditionA || conditionB || conditionC;
    return result;
}
//预分配变量条件检查
函数isSomething(){
const condition a=1>2 | | a'='a'| | 2<4;//一些复杂的条件
const conditionB=55==1 | | |(32>4 | | |'a'='a')&&6>2;//一些复杂的条件
const condition c='abc'!='bca'&&3==3//一些复杂的条件
常量结果=条件A | |条件B | |条件C;
返回结果;
}
在选项2中,它似乎必须检查所有3个条件,但在选项1中,理论上它可以在第一次检查后返回,如果它是
true

显然,选项2是我的选择,因为它更具可读性,尽管我想知道在行为或性能上是否存在差异?
有没有办法在这两个选项之间测试性能

至于检查性能,我会看看

另外,如果您还没有看到,请查看
console.time()
console.profile()
,以及
performance.now()

在选项2中,您创建3个新对象并将其分配给变量,创建对象并在内存中分配它们,这对性能的影响往往是微不足道的

在选项1中,如果第一个值为真,则第二个选项将不会被评估为短路运算符,而在第二个选项中,无论返回的结果如何,都将评估所有三个条件


如果性能是一个问题,因为这种方法被多次使用,我总是建议性能测试尽可能模拟真实世界的应用程序。

如果您想将短路评估的好处与可读性和命名变量结合起来,那么

function isSomething(){
  const conditionA = () => 1 > 2 || 'a' == 'a' || 2 < 4;
  const conditionB = () => 55 == 1 || (32 > 4 || 'a' == 'a') && 6 > 2;
  const conditionC = () => 'abc' != 'bca' && 3 == 3;

  const result = conditionA() || conditionB() || conditionC();

  return result;
}
函数isSomething(){
const condition a=()=>1>2 | | a'='a'='a'| | 2<4;
const conditionB=()=>55==1 | | |(32>4 | | | | a'==a')&&6>2;
const condition c=()=>'abc'!='bca'&&3==3;
const result=conditionA()| | conditionB()| | conditionC();
返回结果;
}

至于性能测试,如果您还没有听说过jsPerf,这可能是访问该站点的最佳时机。有多种替代方案,都在你的浏览器中在线(例如)。@WiktorZychla很有趣,谢谢,我不知道。虽然有时我会得到不同的结果。例:代码块1最快,然后代码块2最快当你在两个选项中进行相同的条件检查时,这是不正确的。正如OP本人所指出的,第一种情况会短路,而第二种情况不会。@torazaburo我看到在第一种情况下,如果第一种情况是正确的,那么其他情况将不会运行,而在第二种情况下,它们将运行。我将修改答案。我不确定这是否符合我问题的确切答案,但这个解决方案很棒!没有考虑过,有点让它“懒惰”的条件检查。谢谢。