Efficiency

Reading Kyle Simpson’s article about Parens and Performance put in me in mind of this story:

Back in 1977, I was a system programmer at the University of Delaware. One day, as I walked past one of the labs, I saw two high school students having some sort of argument/discussion. They called me in and showed me two segments of BASIC code that they had written on the board. “Which one is faster?” they asked me.

I took a look, and I couldn’t see any compelling advantage that one method had over the other. I told the students as much, and hedged my bets by saying, “Without looking at the code that the compiler generates, I can’t give a definitive answer." As I turned to walk out, a thought struck me. “Let’s say that one of these programs takes, oh, five milliseconds longer than the other one.” (Even in 1977, five milliseconds was a lot of time.) “How many times are you planning to run this program?”

“Maybe 15 times or so,” said one of the students.

“So the total CPU time one program will save over the other is less than a tenth of a second. How long have you been arguing about this?” I asked.

“About twenty minutes,” replied the other student, at which point I gave them a look of “well then, there you have it” as they stared at each other with a look of realization dawning on their faces.

The point is this: as a programmer, why are you spending your time worrying whether to put in the parentheses or not in an IIFE (immediately invoked function expression)? Instead, I would ask:

So, in summary, I agree with Kyle. Leave out the parentheses, make your code clear to the rest of us humans, and let the compiler writers work their wizardry to do the optimization for us.