Ad
  • Custom User Avatar

    That’s a very interesting point, thanks. I want to clarify though, since you say "call" and "reevaluate", that this not calling a function but accessing a property of the (immutable) string.

    How this is handled internally by the JS engine is a different question; there’s a more in-depth discussion that touches on this on StackOverflow. In short, caching .length has no effect on real-world performance in V8, Firefox, etc.

    (Note that in this case the entire issue can be avoided by using e.g. str.slice(0, -1).)