1

Given:

var x = 2;

if (x >= 1)
    // do stuff

if (x > 0)
    // do stuff

Both conditions will be true, but is there a difference in terms of performance? Should one be used over the other in terms of standardization?

jsFiddle

FastTrack
  • 8,810
  • 14
  • 57
  • 78
  • I guess the firs one compares two thing therefore will be slower. But I don't think this can affect performance. – Jacob Jan 08 '14 at 13:51
  • 2
    I think the answer might be here: http://stackoverflow.com/questions/5861222/comparison-operator-performance – Jacob Jan 08 '14 at 13:53
  • 1
    Use whichever one makes the code more readable. You will see a negligible performance difference across all browsers and the only way you would notice it is if you are looping a `for()` loop a couple million times because those picoseconds will really add up! – MonkeyZeus Jan 08 '14 at 13:58
  • Use http://jsperf.com/ and find out... – epascarello Jan 08 '14 at 13:59

2 Answers2

1

I personally prefer x > 0, because it is easier to read.

In terms of performance I'm almost confident that they're equal. That being said, I think the most important thing is to consistently use either style.

-1

The >= means if the first statement is larger of equal to. The > means if the first state ment is larger.

for example:

x=4;

if( x >= 4 ){
  echo "This is TRUE since x is bigger OR equal to 4";
}
if( x > 4 ){
  echo "This is FALSE since x isn't bigger than 4, it is 4.";
}

if( x <= 4){
  echo "This is TRUE since x is smaller or equal to 4.";
}

if( x < 4){
  echo "This is FALSE since x isn't smaller than 4.";
}
Lallex
  • 100
  • 4