What statement is faster in javascript?🤔 I need real proofs, guys. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

What statement is faster in javascript?🤔 I need real proofs, guys.

1. x++ 2. ++x 3. x = x + 1

15th Apr 2019, 3:51 AM
Вап
13 Answers
+ 19
I don't know much about javascript but pre increment has more fast in performance. I am getting this by following this approach First for x++:- It stores the value of x first then make copy of that number and then increment it when occurs same variable again so 3 clock cycle are there one for store the other copy of the number then increment that number. So 3 is performace unit for the post increment Now for ++x It directly increment the value in the variable and store it so only 2 cycle 1 for increment and other for store the value. Now for x = x + 1 It first take an value in variable then perform addition then assign to the variable at left side so it too take 3 unit for performance. So it is similar as post increment. If you are using x+=1 then take value in x and 1 to it only 2 step and it will equal to the pre increment.
15th Apr 2019, 4:10 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 12
4rontender in C++ at the back-end when source code is converted into assembly level then the code is in its optimized form as in compiler, compiler has important layers, lexical analyzer, syntax analyzer, semantics analyzer, code optimizer. In code optimizer all this source code statement is converted in most optimized code by using 3 address variables code and different parsing algorithm. But in the front level pre increment is faster as it takes no time to increment value. Post increment is slow as compare too pre increment like in javascript. x+=1 is used as it is quick too read by compiler from left to right as variable x is only need to match in symbol table of memory one time so it is faster. And in x=x+1 the variable x needs to match twice so it is take much time to execute. Thus += is used in general.
15th Apr 2019, 4:42 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 11
This link has some information in that regards. https://stackoverflow.com/questions/12479486/is-x-a-quicker-than-x-x-a
15th Apr 2019, 4:43 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 6
Thanks, GAWEN STEASY for detailed response. What about c++?
15th Apr 2019, 4:17 AM
Вап
+ 6
High level language compilers are smart enough to optimise the codes now. I tried to run following C# tests on LINQPad 5, they all generate the same assembly codes. int x=1; x++; int x=1; ++x; int x=1; x=x+1; int x=1; x+=1; Assembly code output: IL_0000: nop IL_0001: ldc.i4.1 IL_0002: stloc.0 // x IL_0003: ldloc.0 // x IL_0004: ldc.i4.1 IL_0005: add IL_0006: stloc.0 // x IL_0007: ret
15th Apr 2019, 5:10 AM
Calviղ
Calviղ - avatar
+ 6
In today's compilers all ASM code is made very identical for above mention operation pre post increment and x+=1, x=x+1 https://dev.to/somedood/the-difference-between-x-and-x-44dl
15th Apr 2019, 6:38 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 5
For real proof you can take the datetime intervals of any operation 🤔
15th Apr 2019, 4:08 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
I just want to add that the difference (if it exists) is probably in the sub-nanosecond range, so it really doesn't matter at all. Especially in javascript where we are loading tons of images and css and whatnot alongside the code, which takes about a billion nanoseconds. Calviղ's snippet even gives me different results the more often I run it!
15th Apr 2019, 5:04 AM
Schindlabua
Schindlabua - avatar
15th Apr 2019, 4:17 AM
Calviղ
Calviղ - avatar
+ 4
Calviղ Strange assembler🤔 c#?
15th Apr 2019, 5:14 AM
Вап
+ 3
4rontender It is Intermediate Language, IL of c#, the lower level of it. The IL has opcodes that manipulate the stack. Here I've added the comments of the operation of these opcodes: IL_0000: nop IL_0001: ldc.i4.1 // push the value of 1 onto stack, x IL_0002: stloc.0 // pop the value from the top of the stack IL_0003: ldloc.0 // load the local variable onto stack IL_0004: ldc.i4.1 // push value 1 (increment value, i) onto stack IL_0005: add // add 2 values (x+i) and add the result onto stack IL_0006: stloc.0 // pop the current value from stack IL_0007: ret
15th Apr 2019, 5:37 AM
Calviղ
Calviղ - avatar
+ 2
++x is fastest x++ is slowest
15th Apr 2019, 4:07 AM
Calviղ
Calviղ - avatar
+ 2
Schindlabua yes, it generates different results when we run more.. In JavaScript, what I can observe is x++ is always the slowest. The speed of the rest are closely the same. Anyway you're right, we don't actually concerns too much about the speed different between increment operator, it's not significant if compare to other operations.
15th Apr 2019, 5:14 AM
Calviղ
Calviղ - avatar