user3324865
user3324865

Reputation: 163

Which of these two JS lines costs less?

Assume that booleanMatrix[x][y][z] is true 10% of the time. Which of the alternatives are more efficient?

if (booleanMatrix[x][y][z]) booleanMatrix[x][y][z] = false; // alternative 1
booleanMatrix[x][y][z] = false; // alternative 2

The two alternatives give the same functionality [edit] in the context of my program [/edit]. Alternative 1 first checks the value before possible rewriting it. Alternative 2 just goes straight ahead and writes it. So, i suppose this boils down to whether the cost of accessing the value of a variable is smaller than the cost of editing it. Is it?

EDIT: Elsewhere in my program, an undefined booleanMatrix[x][y][z] is treated as if it were false. This is why alternative 1 does not give ill consequences in my program.

Upvotes: 0

Views: 71

Answers (4)

Levi Lindsey
Levi Lindsey

Reputation: 1068

There are really slick benchmarking tools out there that are great for things like this. Specifically, I would recommend jsperf. Here is a test I wrote up for this scenario: http://jsperf.com/read-or-write.

After running this test, the conclusion is that writing without testing is, in general, MORE EFFICIENT option.

NOTE: Some of my test results conflict with those from Renato (http://jsperf.com/testing-before-vs-just-writing). The reason for this likely relates to his test using a different ratio of trues and falses. Or maybe to his test using a single-dimension array while my test uses three-dimensional arrays.

So, you should notice that this result is NOT very reliable. It seems that there are many other factors that are actually more significant than the specific question you are asking. These results might vary a great deal with arrays of different dimensions, with arrays of different ratios of true and false, with different browsers, etc.

It seems that ultimately, the performance difference is not especially significant. However, you could always write additional tests that are more specific to your scenario in order to get some more definitive results.

Upvotes: 1

Alex W
Alex W

Reputation: 38173

Alternative 1 will always take slightly more time to execute because it has to run through the logic of the if(). The only cases where these would be equal are:

  • if the if() statment took no time to evaluate.
  • or if the JavaScript engine optimized alternative 1 to be equal to alternative 2

Upvotes: 0

user3417400
user3417400

Reputation: 391

Alternative 2 is of course more efficient,

Break those two alteratives in 2 different operations:

if (booleanMatrix[x][y][z])

and

booleanMatrix[x][y][z] = false;

The first alternative is operation 1 + operation 2, and the second alternative is operation 2, therefore, alternative 2 will always be faster, the question is, does it work for what you need?

Upvotes: 0

Renato Zannon
Renato Zannon

Reputation: 29941

Writing without testing before seems to be faster: http://jsperf.com/testing-before-vs-just-writing

Upvotes: 0

Related Questions