0

I have been wondering recently if accessing the array.length getter was cached by NodeJS, but I have only been able to find conclusive answers regarding JS interpretation in browsers, but I am developping apps in Typescript, so that's not relevant to my question (correct me if I'm wrong)

So, is accessing array.length of time complexity O(1) (cached by NodeJS) or O(n) (not cached by NodeJS) in NodeJS 14 ?

I have a hunch that it is cached, but I'd really like a definitive answer or "proof" of it

Ocodenyth
  • 15
  • 3
  • 1
    It can't be cached except in very specific circumstances where there's a tight block of code that the interpreter can see everything that is happening locally and optimizes the code (like unrolling a loop) because as long as the array could be modified, the length can change which would cause caching to give false results. Array.length is essentially a property access - it's not a 100% normal property because its value is automatically adjusted by other array methods, but accessing it is essentially a highly optimized property access. So, it would be an optimized version of O(1). – jfriend00 Jun 10 '21 at 08:04
  • 2
    FYI, nodejs uses the same V8 Javascript engine that the Chrome browser uses so the answer to your question will be the same for both. – jfriend00 Jun 10 '21 at 08:07
  • 1
    If what you meant to ask is whether the `.length` property is calculated from scratch by counting the array elements every time, that is definitely NOT how it works. It is an internal property of the Array object that is maintained as the array is modified and accessing the `.length` property, just retrieves that internal property value. If that's what you meant by cached, then yes it is cached. I originally thought you were asking if the JS engine itself caches it outside of the Array object. – jfriend00 Jun 10 '21 at 08:09
  • Yes that's what I meant by "cached" Thanks for the clarification regarding the V8 engine ! – Ocodenyth Jun 10 '21 at 11:48

1 Answers1

1

If what you meant to ask is whether the .length property is calculated from scratch by counting the array elements every time, that is definitely NOT how it works. It is an internal property of the Array object that is maintained as the array is modified. Accessing the .length property, just retrieves that internal property value. If that's what you meant by cached, then yes it is cached.

There also may be specific circumstances where the optimizing compiler can be sure that your array is not being modified (such as a for loop iterating the array with only local code that does not modify the array) where the compiler itself may cache the .length property outside of the array (but apparently that is not what you meant to ask about).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Afaik, the optimising compiler also caches it outside of the array object in `for (let i=0; i – Bergi Jun 10 '21 at 23:14
  • Ah, no idea why I skipped reading the first comment. I suspect it still could be closed as dupe of https://stackoverflow.com/questions/5752906/is-reading-the-length-property-of-an-array-really-that-expensive-an-operation – Bergi Jun 10 '21 at 23:21
  • I felt it was relevant as I am specifically asking about NodeJS, which none of the other issues I found seemed to mention, and from what I understand this is a question that will depend on the interpreter (browsers for "web" js, or NodeJS for "application" js) – Ocodenyth Jun 11 '21 at 11:34