The difference in performance between + =, ++, +

I created this test http://jsperf.com/loop-counter , why there is such a difference between these three expressions.

+7
source share
4 answers

because your test is wrong. you reuse the same variable, so the more it gets, the slower it grows. take a look at this: http://jsperf.com/loop-counter/6

This is how jsperf works - the preparation code is run only once, before all tests.

+9
source

I tried to run all three tests several times, and every time I reload the page, the first test I'm trying to do is the fastest to date.

So, I assume that there is some problem with a test too short, that is, the code that runs the tests takes most of the time.

+2
source

If this is not a rhetorical question, and you really want awer then: because of how people wrote the JS engine in browsers.

+2
source

This is because of what the program does behind the scenes:

l_count + = 1; This adds the number 1 to the variable.

l_count = l_count + 1; This calls the l_count variable, reads it, adds 1 to the result, and passes it back to l_count.

l_count ++; This adds 1 to the variable after starting the line. Thus, the value is stored in another temporary variable during the execution of the line, then the value is returned, 1 is added and saved back to the original value.

+2
source

All Articles