0

I noticed I can use a && b(); as a shorthand for if(a) {b()} or if(a) b();. The MOST usual case for this indecision is for objects having 4-5 elements.

Question: is there a performance gain/loss for using the shorthand when compared with an iteration of if(){} or regular object traversing for (var i in b) { b[i]() } ?

As suggested: some code

var b = { x: 20, y: 30 }, // sample object
    fn = function(v){ return v+5; }, // sample function
    endValue = 0; // some lol

// go through all elements via if
if ('x' in b) { endValue += fn(b['x']); }
if ('y' in b) { endValue += fn(b['y']); }

//go through all elements via shorthand
('x' in b) && (endValue += fn(b['x'])); 
('y' in b) && (endValue += fn(b['y']));

//go through all elements via object traversing
for (var i in b) {
  endValue += fn(b[i]);
}

I would assume there could be very very negligible performance differences, I need to use the above shorthand method to have a very specific order of the object elements into the computing of endValue.

thednp
  • 4,135
  • 3
  • 27
  • 41
  • 3
    Why don't you benchmark if yourself and see? – Matt Burland Sep 01 '16 at 13:26
  • What does this have to do with iteration/enumeration? – Bergi Sep 01 '16 at 13:27
  • I did all tests with conditionals and object traversing, I never tried the shorthand yet, currently working on it, I wish to know if it's worth the effort, it's alot of code to read. – thednp Sep 01 '16 at 13:28
  • @Bergi I don't actually know what is called a list of consecutive `if`s. – thednp Sep 01 '16 at 13:29
  • 2
    Minifiers tend to convert some statements to this to reduce size. I think the performance would be negligible, but doesn't answer your question. I recommend sticking with the use of if statements for readability. – Curt Sep 01 '16 at 13:29
  • 1
    @thednp: I don't see any lists of consecutive statements in your question. Please post actual code; otherwise I'm tempted to close as a duplicate of [this question](http://stackoverflow.com/q/12664230/1048572) – Bergi Sep 01 '16 at 13:30
  • *"it's alot of code to read"* why would it be a *lot* of code? Although it's not entirely clear how you are relating `if` to traversing an object – Matt Burland Sep 01 '16 at 13:33
  • Hold on, I am writing some sample code. – thednp Sep 01 '16 at 13:33
  • @Bergi I edited the question, added some sample code and a reason why I need the shorthand. – thednp Sep 01 '16 at 13:46
  • @thednp: Your shorthand seems to miss the `&&` operator – Bergi Sep 01 '16 at 13:47
  • @Bergi indeed, updated. – thednp Sep 01 '16 at 13:50

2 Answers2

2

Is there a performance gain/loss for using the shorthand when compared with an if(){}?

No. With any decent compiler they perform absolutely the same. You should however use the if statements for readability and conciseness.

Is there a performance gain/loss for using an iteration of if(){} vs regular object traversing for (var i in b) { b[i]() }?

That cannot really be compared as they are doing fundamentally different things, and assuming they have the same outcome the performance depends a lot on the actual objects you pass in and their properties. Use the one whose behaviour you need.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • Just FYI: I finished testing and it seems object traversing is faster than the other two, no matter what. Perhaps a 10-15% performance loss. – thednp Sep 01 '16 at 23:45
0

closure compiler by google extensively uses shorthand codes and from benchmarks there was minor to none performance gains. https://closure-compiler.appspot.com/home

snit80
  • 553
  • 4
  • 13