1

Which is better optimized code and why? CodeBlock1 is more readable however it is consuming two variables which is not needed. However CodeBlock2 is less readable but looks very optimized.

function CodeBlock1(x,y,z,n,m,p)
{
var a= x || y || z; 
var b= n || m || p; 
var c = a || b; 
return c 
}

//OR 

function CodeBlock2(x,y,z,n,m,p)
{
return x||y||z || n ||m||p;
}
7
  • Just looking at them visually, the first one would have three additional variables defined. Other than that, performance wise, I doubt there is a noticable difference. Commented Jan 18, 2019 at 19:37
  • Actually that's not true. Code block 1 will do more work. As it will always perform three operations, to determine the value of a, b, and c. Where as the second block can perform at most 1 operation, if the x is truthy Commented Jan 18, 2019 at 19:38
  • I think CodeBlock1 makes it look more complex than it is, at the end if only 1 or the paramaters is true the return value will be true and CodeBlock2 is more readable for that logic also more optimized. Commented Jan 18, 2019 at 19:39
  • space complexity is lower In code 2, because less variables, but the time complexity is the same in both the codes because they always perform five checks. Code 1 can offer a higher readability when the checks are grouped 3 by 3 and you can distinguish then by cases (isSomething || isSomethingElse) Commented Jan 18, 2019 at 19:45
  • In general, I never trust anybody's opinion of what's more optimized (though this case is pretty simple and I think all this comments are accurate) If you have a question performance, benchmark it! jsben.ch/witaM Commented Jan 18, 2019 at 19:46

2 Answers 2

2

Generating bytecode for the two approaches give the following results:

CodeBlock1

[generated bytecode for function: CodeBlock1]
Parameter count 7
Frame size 24
   81 E> 0x1e25e29944ba @    0 : a1                StackCheck
  106 S> 0x1e25e29944bb @    1 : 25 07             Ldar a0
         0x1e25e29944bd @    3 : 92 08             JumpIfToBooleanTrue [8] (0x1e25e29944c5 @ 11)
         0x1e25e29944bf @    5 : 25 06             Ldar a1
  111 E> 0x1e25e29944c1 @    7 : 92 04             JumpIfToBooleanTrue [4] (0x1e25e29944c5 @ 11)
         0x1e25e29944c3 @    9 : 25 05             Ldar a2
         0x1e25e29944c5 @   11 : 26 fb             Star r0
  128 S> 0x1e25e29944c7 @   13 : 25 04             Ldar a3
         0x1e25e29944c9 @   15 : 92 08             JumpIfToBooleanTrue [8] (0x1e25e29944d1 @ 23)
         0x1e25e29944cb @   17 : 25 03             Ldar a4
  133 E> 0x1e25e29944cd @   19 : 92 04             JumpIfToBooleanTrue [4] (0x1e25e29944d1 @ 23)
         0x1e25e29944cf @   21 : 25 02             Ldar a5
         0x1e25e29944d1 @   23 : 26 fa             Star r1
  151 S> 0x1e25e29944d3 @   25 : 25 fb             Ldar r0
         0x1e25e29944d5 @   27 : 92 04             JumpIfToBooleanTrue [4] (0x1e25e29944d9 @ 31)
         0x1e25e29944d7 @   29 : 25 fa             Ldar r1
         0x1e25e29944d9 @   31 : 26 f9             Star r2
  169 S> 0x1e25e29944db @   33 : a5                Return
Constant pool (size = 0)
Handler Table (size = 0)

CodeBlock2

[generated bytecode for function: CodeBlock2]
Parameter count 7
Frame size 0
   81 E> 0x1b1ed00944ba @    0 : a1                StackCheck 
   99 S> 0x1b1ed00944bb @    1 : 25 07             Ldar a0
         0x1b1ed00944bd @    3 : 92 14             JumpIfToBooleanTrue [20] (0x1b1ed00944d1 @ 23)
         0x1b1ed00944bf @    5 : 25 06             Ldar a1
  109 E> 0x1b1ed00944c1 @    7 : 92 10             JumpIfToBooleanTrue [16] (0x1b1ed00944d1 @ 23)
         0x1b1ed00944c3 @    9 : 25 05             Ldar a2
  112 E> 0x1b1ed00944c5 @   11 : 92 0c             JumpIfToBooleanTrue [12] (0x1b1ed00944d1 @ 23)
         0x1b1ed00944c7 @   13 : 25 04             Ldar a3
  117 E> 0x1b1ed00944c9 @   15 : 92 08             JumpIfToBooleanTrue [8] (0x1b1ed00944d1 @ 23)
         0x1b1ed00944cb @   17 : 25 03             Ldar a4
  121 E> 0x1b1ed00944cd @   19 : 92 04             JumpIfToBooleanTrue [4] (0x1b1ed00944d1 @ 23)
         0x1b1ed00944cf @   21 : 25 02             Ldar a5
  126 S> 0x1b1ed00944d1 @   23 : a5                Return 
Constant pool (size = 0)
Handler Table (size = 0)

The first one is 'more complex' in that there is intermediate storage on the heap instead of just using the stack like the second one does. CodeBlock1 uses both more space and more instructions.

Is CodeBlock2 therefore "more optimized?" It depends on what you want to optimize for.

  • CodeBlock1: easier to read and easier to debug with a debugging tool
  • CodeBlock2: takes up less memory and executes fewer instructions

Try this yourself using node --print-bytecode index.js, assuming the name of your script is index.js

Sign up to request clarification or add additional context in comments.

Comments

1

basically you have to think that if you have a || b || c || .... || X (x number of comparisons), at the worst case you will need to check X number of times and the less is checking once (a is truthy).

this is because how || works, it will stop at the first TRUE statement.

so

function CodeBlock1(x,y,z,n,m,p){
    var a= x || y || z;  //this will check 1, 2 o 3 times
    var b= n || m || p;  //this will check 1, 2 o 3 times
    var c = a || b; //this will check 1 or 2 times.

    //worse case you will check 3 + 3 + times.
    return c;
}


function CodeBlock2(x,y,z,n,m,p){
    // this will check 1, 2, 3, 4, 5 or 6 times
    return x || y|| z || n || m || p;
}

so basically the worse case for CodeBlock2 is 6 times and the worse for CodeBlock1 is 8. also you are defining more variables on the CodeBlock1 so maybe that will add more load.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.