0

(I'm assuming the old value of i is not needed, so i++ and ++i should at most affect performance.)

The thing is, I've seen a counter incremented like this in code

i++;

and it left me wondering: should I suggest to write ++i; because given the semantics of pre-increment and post-increment there's no reason to believe that ++i could ever be slower than i++, whereas there is a little chance that i++ is indeed slower?

My double stems from what I know of C++, and which mostly applies for non-builtin types (see here).

But I don't know enough of JavaScript.

As state above, exactly because I don't know the answer as whether i++ is slower than ++i or not, I'd write ++i because I assume it can't be slower than i++ (otherwise what starnge language would JavaScript be?).

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 3
    JavaScript, as a language does not have performance characteristics. It all depends on the JavaScript engine you use, much in the same way that performance differences in pre-increment and post-increment in C very much depend on the compiler that is used. If you've identified a JavaScript engine that you want to target, just write a benchmark and test it yourself. – Robby Cornelissen Apr 26 '23 at 09:03
  • 6
    If you want to optimize your code `i++` or `++i` probably isn't the first place to look – Reyno Apr 26 '23 at 09:04
  • @Reyno, given standalone `i++;` changing it to `++i;` has no cost and, if I know that the latter can't be slower than the former, then I'd write the latter everytime, I would not start searching for `i++;`s when the performance becomes a concern. – Enlico Apr 26 '23 at 09:28
  • @RobbyCornelissen, doesn't that hold for any language? You yourself mention C. I can add C++. And any other. – Enlico Apr 26 '23 at 09:30

1 Answers1

1

Thinking about Javascript in these kinds of low-level performance terms is not really useful. Different runtimes on different hardware will change how your code is interpreted and how it performs. You don't want to tie your code to a specific platform, runtime, or version, so there's no point worrying about this. If you are incrementing a variable enough times you need to worry about the performance of the increment operator, there is a good chance the problem is your approach rather than the operation.

In general i++ is standard and more readable, ++i is useful for when you want to access the value of i after it has been incremented. The standard approach would be to stick with what is most readable unless you have a good reason not to.

glenatron
  • 11,018
  • 13
  • 64
  • 112
  • Are you claiming that it is not guaranteed that `++i` is never slower `i++` regardless of any factors? – Enlico Apr 27 '23 at 05:41
  • @Enlico Confusingly phrased, but I think so - there is no guarantee that any operator will behave in any specific way beyond the standard. The factors that control which is faster are numerous and entirely beyond our control as developers, so there's no point worrying about it. Also if you are writing any non-trivial Javascript then the thing which slows your code down will not be the performance of the increment operator. We're not working with bare-metal assembler here, Javascript is one of the highest-level languages around. – glenatron Apr 27 '23 at 09:47
  • As high a language JS can be, `i++` requires that the old value is still available after `i` has been incremented, i.e. there's a moment during execution where the 2 values somehow need to be both accessible, whereas `++i` requires that only the new value is available. I don't see how any implementation in which `++i` would be slower than `i++` would not be bugged as it would be wasting more time for less work. – Enlico Apr 27 '23 at 09:54
  • _the thing which slows your code down will not be the performance of the increment_, yes that's pretty clear, but my question is not about optimizing anything. – Enlico Apr 27 '23 at 09:56