• Register
499 points
8 4 3

What is the difference between pre and post?

The prefixes "pre-" and "post-" refer to events before and after. For instance, "pre-season" and "post-season" or "pre-study" and "post-study.

This is what I read and could answer your question: "preincrement (++i) adds one to the value of i, then returns i; in contrast, i++ returns i then adds one to it, which in theory results in the creation of a temporary variable storing the value of i before the increment operation was applied"

This is a faux optimization. As far as I understand it, you're saving 1 op code. If you're looking to optimize your code with this technique, then you've gone the wrong way. Also, most compilers/interpreters will optimize this for you anyway  In short I wouldn't worry about. But, if you're really worried, you should use i+=1.

Here's the quick-and-dirty benchmark I just did

var MAX = 1000000, t=0,i=0;

t = (new Date()).getTime();
for ( i=0; i<MAX;i++ ) {}
t = (new Date()).getTime() - t;

console.log(t);

t = (new Date()).getTime();
for ( i=0; i<MAX;++i ) {}
t = (new Date()).getTime() - t;

console.log(t);

t = (new Date()).getTime();
for ( i=0; i<MAX;i+=1 ) {}
t = (new Date()).getTime() - t;

console.log(t);

More Posts

Difference between pre increment and post increment Sanjana Sagar - May 22
What is the difference between oltp and olap Sanjana Sagar - May 20
What is the difference between padding and margin Sanjana Sagar - May 28
What is the difference between sql and tsql Sanjana Sagar - May 20
What's the difference between alpha and beta Sanjana Sagar - May 20
What is the difference between ascii and unicode Sanjana Sagar - May 28
What is the difference between thread and process Sanjana Sagar - May 28
What is the difference between a stack and a queue Sanjana Sagar - May 28
What is the difference between varchar and nvarchar Sanjana Sagar - May 22
What is the difference between id and class in css Sanjana Sagar - May 22