From: Lasse Reichstein Nielsen on
Spamless <Spamless(a)Nil.nil> writes:

> I was writing a little recursive routine in Javascript (it's a simple
> language and I have the spider monkey interpreter) needing what was
> effectively a two-dimensional array and originally used an object
> (keys: ""+i1+","+i2)
>
> In use, parameters were 100 to 500 and ... well, it was OK, but a
> bit slow so I implemented it as a one dimensional array with
> _indexer (one-one map from whole number pairs to whole numbers)
> and that sped it up *quite* dramatically.

Unsurprisingly, one might add.

> It seems that with thousands of keys, Javascript is not too
> efficient in searching the key space to get a value. Perhaps I
> should have tried perl!

Have you considered the overhead of string concatenation for
each lookup? That is likely to take at least as long as doing
the actual lookup, and it creates a lot of garbage strings that
will trigger garbage collection more often.

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Ry Nohryb on
On May 2, 1:53 pm, Spamless <Spaml...(a)Nil.nil> wrote:
> On 2010-05-01, Evertjan. <exjxw.hannivo...(a)interxnl.net> wrote:
>
>
>
>
>
> > VK wrote on 01 mei 2010 in comp.lang.javascript:
>
> >> Indeed I was getting nuts by trying to visualize that "5-dimensional
> >> array of 72 elements" because I hate to use things I do not "see" even
> >> if they work properly.
>
> > Having a 5 dimensional array, which each array maximized at 1000 is easy,
> > if you do not fill the array to much.
>
> > Use an object [here 3 members are used]:
>
> ><script type='text/javascript'>
>
> > var my5dim = {
> >  '000!235!148!999!400':'qwerty',
> >  '074!088!002!000!874':'asd',
> >  '000!088!148!999!401':'blah'};
>
> I was writing a little recursive routine in Javascript (it's a simple
> language and I have the spider monkey interpreter) needing what was
> effectively a two-dimensional array and originally used an object
> (keys: ""+i1+","+i2)
>
> In use, parameters were 100 to 500 and ... well, it was OK, but a
> bit slow so I implemented it as a one dimensional array with
> _indexer (one-one map from whole number pairs to whole numbers)
> and that sped it up *quite* dramatically. It seems that with thousands
> of keys, Javascript is not too efficient in searching the key space
> to get a value. Perhaps I should have tried perl!

That may have been so in the past, but it seems that it's not so
anymore in most current browsers: this snippet tests lookup of 1e6
elements in an array vs lookup in an object, and the results in my Mac
are:

FF 3.6:
array lookup: 258 ms
object lookup: 258 ms
Chrome:
array lookup: 7 ms
object lookup: 6 ms
Opera:
array lookup: 459 ms
object lookup: 424 ms
Safari:
array lookup: 9 ms
object lookup: 577 ms

http://jorgechamorro.com/cljs/098/

<script type="text/javascript">
onload= function () {
//debugger;

function log (txt) {
document.body.appendChild(
document.createElement('p')).innerHTML= txt;
}

var timer= (function () {
var time;
return function timer (p, msg) {
switch (p) {
case "start":
return (time= +new Date());
break;
case "stop":
return log((msg || "")+ (new Date()- time)+ " ms");
break
}
}
})();
var array= [];
var object= {};
var kMax= 1e6;
var n= kMax;
var acumulador= 0;

while (n--) {
object[n]= array[n]= n%2 ? 1 : -1;
}

n= kMax;
timer('start');
while (n--) {
acumulador+= array[n];
}
timer('stop', "array lookup: ");

n= kMax;
timer('start');
while (n--) {
acumulador+= object[n];
}
timer('stop', "object lookup: ");

};
</script>
--
Jorge.