Subj : Re: Hook into Array To : Niklas Lindquist From : Brendan Eich Date : Wed Apr 21 2004 01:02 pm Niklas Lindquist wrote: > Is there a way to hook into predefined classes e.g. the Array class to get > notifications when individual objects are inserted/reassigned/deleted, or > when the entire Array is sorted or reversed? > > Also, is there a way to not initialize array elements until its values are > requested? mccabe started some work #if'd JS_HAS_SPARSE_ARRAYS that was never finished. I wonder if he's interested still. This work is not needed to avoid initialization costs, however. It's really intended to keep "holes" in arrays from being filled in with undefined-valued slots and properties naming those slots. That hole-filling can occur due to a number of Array methods. > Currently I'm initializing all elements right after the > JS_NewArray call, which is rather time consuming when only a few elements > actually are needed in the script. My arrays could contain 100,000+ > elements. The only idea I've came up with so far is to skip the Array > alltogther, and expose a function returning objects by index. You don't need to initialize all the elements. "Holes" will appear to have value undefined, and getProperty on indexes for which no property has been set or predefined will not allocate a property or slot. So you should not initialize any elements to undefined. > By the way, are there any constraints regarding the number of elements to > the Array class, except runtime memory? length must fit in a 32-bit unsigned integer. Systems these days can have more VM than 4GB. /be .