javascriptnode.jstyped-arraysmemory-consumptiontypedarray

Will Uint32Array and other typed arrays already claim space when initialised?


I'm trying to come up with an accurate measure of how much memory a particular datastructure will consume. Big part of that is ~ 1Million Uint32Array and 1M BigUint64Array both of max 200 elements.

Is my thinking correct that typed arrays that are initialised to a particular size already consume memory even without elements having been inserted, and that inserts don't change the amount of memory allocated?

If so, I can quickly get a sense of memory needed.


Solution

  • Yes. Constructing a new typed array initialises it with a buffer of the respective size, which allocates the memory.

    In theory, an engine could defer the allocation until the buffer is actually used to store values, but I haven't seen such an optimisation anywhere. It probably happens on the OS level anyway when paging the virtual memory.