Is there any performance difference between i ++ and i ++ in JavaScript?

I am reading. Is there a performance difference between i ++ and ++ i in C? :

Is there any performance difference between i ++ and ++ i if the result value is not used?

What is the answer for JavaScript?

For example, which of the following is better?

1)

for(var i=0;i<max;i++){ //code } 

2)

 for(var i=0;i<max;++i){ //code } 
+4
source share
2 answers

Here is an article about this topic: http://jsperf.com/i-vs-i/2

++i seems a little faster (I tested it on firefox), and one of the reasons, according to the article, is this:

with i ++, before you can increase i under the hood, you need to create a new copy of i. Using ++ i, you do not need this extra copy. i ++ will return the current value until i increases. ++ I am returning an incremental version of i.

+7
source

No. There is no difference in execution time. The difference in the two code fragments is when I increase the number.

 for(i = 0; i < max; i++) { console.log(i); } 

In this first example, the results will be given: 0,1,2,3, ..., max-1

 for(i = 0; i < max; ++i) { console.log(i); } 

This second example will give the results: 1,2,3, ..., max

i ++ increments the value after the operation. ++ I increment the value before the operation.

There is no difference in performance other than one that is less than iterations done in ++ i, because the increment is performed before the first operation

-3
source

Source: https://habr.com/ru/post/1435173/


All Articles