Thanks Shanti! I used your idea and created a function to populate both arrays at the same time
function AddItemToTables(H,A,item)
{
if (H[item])
A[H[item]].cnt++;
else {
H[item] = A.length;
A[A.length] = { 'item': item, 'cnt': 1 };
}
}
Above if the item has already been inserted once, H[item] holds its location in A[]. This removes the need to do for (var i in H) and A[] can be sorted after all items have been added.
The reason for using two tables is that if item is a string representing a number then A['17']=1 and A[17]=1 both are stored as an element of the array not a property of the object.
The program has two sets of objects LX={},LXA=[] and LM={},LMA=[] and inserts elements into both. The number of insertion per input item is not 1:1.
It runs out of memory after inserting
[00:30:31.462][028.288] 63269: LXA.length 28614 LMA.length 136129
../mklistlm.js:109 out of memory
I added printfs to the functions spider monkey uses to get memory in js/src/jsutil.h and it does not really run out of memory; some other internal resources is exhausted.
Here are the mods:
static JS_INLINE void* js_malloc(size_t bytes) {
if (bytes < sizeof(void*)) /* for asyncFree */
bytes = sizeof(void*);
void *p = malloc(bytes);
printf("malloc b=%d p=%p \n",bytes,p);
return p;
}
static JS_INLINE void* js_calloc(size_t bytes) {
if (bytes < sizeof(void*)) /* for asyncFree */
bytes = sizeof(void*);
void *p = calloc(bytes, 1);
printf("calloc b=%d p=%p \n",bytes,p);
return p;
}
static JS_INLINE void* js_realloc(void* p, size_t bytes) {
if (bytes < sizeof(void*)) /* for asyncFree */
bytes = sizeof(void*);
void *n = realloc(p, bytes);
printf("realloc b=%d p=%p n=%p \n",bytes,p,n);
return n;
}
static JS_INLINE void js_free(void* p) {
free(p);
printf("free p=%p \n",p);
}
And the tail of the printout
realloc b=32 p=(nil) n=0xaa35ad8
realloc b=16 p=(nil) n=0xb175e50
malloc b=16 p=0x9ef88c0
malloc b=16 p=0xa795b98
realloc b=16 p=0xa795b98 n=0xa795b98
malloc b=28 p=0x9e6b6d8
realloc b=786432 p=0xb6646008 n=0xb6646008
malloc b=44 p=0xa341000
malloc b=16 p=0x9f16610
realloc b=16 p=0x9f16610 n=0x9f16610
malloc b=28 p=0xa8820b8
malloc b=44 p=0xa30d368
malloc b=16 p=0xaa81138
[00:30:31.317][028.143] 63268: LXA.length 28613 LMA.length 136124
[00:30:31.462][028.288] 63269: LXA.length 28614 LMA.length 136129
../mklistlm.js:109 out of memory