2

When we want to know the length of an array we call the length property of JavaScript. But my curious mind just wants to know how actually JavaScript really counts the value of an array internally. What kind of algorithm or methodology it really used for counting the length of an Array when the array holds a large number of elements?

I just randomly create some arrays with large elements of data and it returns the length property within a very short time. It really doesn't affect whatever the size of the array is.

  • 4
    It keeps track of the biggest index assigned. – Pointy Nov 19 '22 at 19:16
  • Maybe this thread might also help https://stackoverflow.com/questions/26943853/javascript-how-length-is-implemented – Tarmah Nov 19 '22 at 19:17
  • 3
    What Pointy said, but just keep in mind that how exactly it works internally is implementation-dependent. It's not dictated in the JS spec so it's up to the implementor how they want to do it. For performance reasons, most implementations will keep track of it live so it doesn't have to be calculated when requested. – jfriend00 Nov 19 '22 at 19:33

0 Answers0