I was browsing Google Code when I chanced upon this project called JSpeed - optimization for Javascript.
I noticed one of the optimization was to change i++
to ++i
in for loop statements.
Before Optimization
for (i=0;i<1;i++) {} for (var i = 0, j = 0; i < 1000000; i++, j++) { if (i == 4) { var tmp = i / 2; } if ((i % 2) == 0) { var tmp = i / 2; i++; } } var arr = new Array(1000000); for (i = 0; i < arr.length; i++) {}
After optimization
for(var i=0;i<1;++i){} for(var i=0,j=0;i<1000000;++i,++j){if(i==4){var tmp=i>>1;} if((i&1)==0){var tmp=i>>1;i++;}} var arr=new Array(1000000);for(var i=0,arr_len=arr.length;i<arr_len;++i){}
I know what pre and post increments do, but any idea how does this speeds the code up?
Pre-increment is faster than post-increment because post increment keeps a copy of previous (existing) value and adds 1 in the existing value while pre-increment is simply adds 1 without keeping the existing value.
JavaScript Increment operator (++ )In the first case (i.e. post-increment) the operator increases the variable var1 by 1 but returns the value before incrementing. In the second case (i.e. pre-increment) the operator increases the variable var1 by 1 but returns the value after incrementing.
No. There is no difference in execution time.
++i returns the value of i after it has been incremented. i++ returns the value of i before incrementing. When the ++ comes before its operand it is called the "pre-increment" operator, and when it comes after it is called the "post-increment" operator.
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 (reference 1). 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);
Raw results
Post Pre += 1071 1073 1060 1065 1048 1051 1070 1065 1060 1090 1070 1060 1070 1063 1068 1066 1060 1064 1053 1063 1054
Removed lowest and highest
Post Pre += 1071 ---- 1060 1065 ---- ---- 1070 1065 1060 ---- 1070 1060 1070 1063 ---- 1066 1060 1064 ---- 1063 1054
Averages
1068.4 1064.2 1059.6
Notice that this is over one million iterations and the results are within 9 milliseconds on average. Not really much of an optimization considering that most iterative processing in JavaScript is done over much smaller sets (DOM containers for example).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With