2

Is there a difference in terms of performance between these code examples?

if (this.game.physics.arcade.overlap(this.player, this.barels) && (this.game.time.now - this.srdicka.timecheck > this.srdicka.frequency) || (this.player.position.y > this.game.height + 100)) {
  this.damage(1);
}

or

if (this.game.physics.arcade.overlap(this.player, this.barels) && (this.game.time.now - this.srdicka.timecheck > this.srdicka.frequency)) {
  this.damage(1);
}

if (this.player.position.y > this.game.height + 100) {
  this.damage(1);
}

I know that both work just fine but I'm not sure which of them is better.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
Marek Lukáš
  • 47
  • 1
  • 7
  • 2
    depend on the logic, you should care about if both check can be true, then the second example would deal 2 damages – Hacketo Mar 17 '16 at 13:59
  • Yes an or passes if there is just one side of the equation being true so the first one would be quicker – johnny 5 Mar 17 '16 at 13:59
  • In this particular case (and most cases), I'd posit that the performance differences would be negligible. That said, in cases where I have multiple `if` conditions, I strive for readability over anything else. – lux Mar 17 '16 at 14:02
  • @lux if this was being ran through a game loop as it seems to be intended although the performance would be miniscule it would happen several times, and just for the knowledge of it you should know an or doesn't full evaluate if the first condition is true – johnny 5 Mar 17 '16 at 14:21
  • @johnny5 So from what others have said, in game loop where I don't need to deal with both of the conditions being true, **it's better to use multiple conditions in one statement** . If I misunderstood your answers please reply me. Thanks :) – Marek Lukáš Mar 17 '16 at 15:53
  • @MarekLukáš, I would say Randy answer makes the most sense, you get the minor optimizations of the if statement while still have readability. but it's really a case by case basis, you wouldn't want to chain 10 if statements just because you could. – johnny 5 Mar 17 '16 at 16:03

3 Answers3

1

I believe it is better to have it all in one rather than broken into two. The reason being is that you'd be hitting one if statement rather than two.

I would make a change to the first one and format it as the following for readability:

if (this.game.physics.arcade.overlap(this.player, this.barels)
                && (this.game.time.now - this.srdicka.timecheck > this.srdicka.frequency)
                || (this.player.position.y > this.game.height + 100))
            {
                this.damage(1);
            }
HappyCoding
  • 641
  • 16
  • 36
  • Think of using these if statements more then once, they are monsterous! I call these Trainwrecks. – Randy Mar 17 '16 at 14:07
1

You could split up the conditions for not only readability, but reusage, maintainability and usability:

if (overlap() && frequency() || position())
{
    this.damage(1);
}

function frequency(){
    return this.game.time.now - this.srdicka.timecheck > this.srdicka.frequency;
}

function overlap(){
    return this.game.physics.arcade.overlap(this.player, this.barels);
}

function position(){
    return this.player.position.y > this.game.height + 100;
}

If your compiler works good, as any will in modern browsers, you only have to care about the && and || notations for performance. || will only evaluate the left side, and if that is false it will continue to the right side. Use those cleverly.

Randy
  • 9,419
  • 5
  • 39
  • 56
1

Yes. In the first version, this...

(this.player.position.y > this.game.height + 100)

...will only be evaluated if this...

this.game.physics.arcade.overlap(this.player, this.barels) &&
(this.game.time.now - this.srdicka.timecheck > this.srdicka.frequency)

...evaluates to false. In the second version it'll be evaluated no matter what. So if the expression that's in the second if in the second example is going to be expensive it could make a significant difference. As noted in the comments, the difference between your two examples also changes the logic. In this case making the second statement an else if would preserve the original logic.

Here's an example that illustrates the difference in performance and logic with direct effects and side effects. Run it and look at the console.

JMM
  • 26,019
  • 3
  • 50
  • 55