Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there ideal array sizes in JavaScript?

I've seen little utility routines in various languages that, for a desired array capacity, will compute an "ideal size" for the array. These routines are typically used when it's okay for the allocated array to be larger than the capacity. They usually work by computing an array length such that the allocated block size (in bytes) plus a memory allocation overhead is the smallest exact power of 2 needed for a given capacity. Depending on the memory management scheme, this can significantly reduce memory fragmentation as memory blocks are allocated and then freed.

JavaScript allows one to construct arrays with predefined length. So does the concept of "ideal size" apply? I can think of four arguments against it (in no particular order):

  • JS memory management systems work in a way that would not benefit from such a strategy
  • JS engines already implement such a sizing strategy internally
  • JS engines don't really keep arrays as contiguous memory blocks, so the whole idea is moot (except for typed arrays)
  • The idea applies, but memory management is so engine-dependent that no single "ideal size" strategy would be workable

On the other hand, perhaps all of those arguments are wrong and a little utility routine would actually be effective (as in: make a measurable difference in script performance).

So: Can one write an effective "ideal size" routine for JavaScript arrays?

like image 384
Ted Hopp Avatar asked Jan 24 '14 21:01

Ted Hopp


2 Answers

Arrays in javascript are at their core objects. They merely act like arrays through an api. Initializing an array with an argument merely sets the length property with that value.

If the only argument passed to the Array constructor is an integer between 0 and 232-1 (inclusive), this returns a new JavaScript array with length set to that number. -Array MDN

Also, there is no array "Type". An array is an Object type. It is thus an Array Object ecma 5.1.

As a result, there will be no difference in memory usage between using

var one = new Array();
var two = new Array(1000);

aside from the length property. When tested in a loop using chrome's memory timeline, this checks out as well. Creating 1000 of each of those both result in roughly 2.2MB of allocation on my machine.

one

two enter image description here

like image 99
Travis J Avatar answered Oct 15 '22 10:10

Travis J


You'll have to measure performance because there are too many moving parts. The VM and the engine and browser. Then, the virtual memory (the platform windows/linux, the physical available memory and mass storage devices HD/SSD). And, obviously, the current load (presence of other web pages or if server-side, other applications).

I see little use in such an effort. Any ideal size for performance may just not be ideal anymore when another tab loads in the browser or the page is loaded on another machine.

Best thing I see here to improve is development time, write less and be quicker on deploying your website.

like image 44
pid Avatar answered Oct 15 '22 12:10

pid