Am 02.08.2006 um 07:14 schrieb Sebastian Werner:

>>> Ah. Cool. Thank you for the information. But this doesn't explain  
>>> why
>>> for some stuff arrays are faster. If arrays really are built up with
>>> hash maps why are they faster for common array actions like loops?
>>
>> Because you can use simple counter loops ("for (i=0;i<x;i++)") to
>> enumerate the keys (instead of "for (x in y)"). I think when you
>> iterate over an array with for/in, you get the same performance as
>> with any other object. I can't say whether there are some more
>> internal optimizations for arrays, but at least semantically they
>> behave like ordinary hash maps (with the exception of having an
>> automatically updated "length" property whenever numeric keys are  
>> used).
>
> But nobody really knows it, or is this explained in any reference.

It's explained in the ECMAScript spec: http://www.ecma- 
international.org/publications/files/ECMA-ST/Ecma-262.pdf (section  
15.4, page 88). This section states that an array simply uses special  
treatment for certain property names (namely numeric ones in a  
certain range), but otherwise it's just an ordinary property  
container like any other JavaScript object.

> Generally they could have a comparable API with completely different
> implementations.

 From reading the spec, I don't think this is true.

> Mhh, I don't see why a counter should be faster than enumerate over  
> the
> keys. I there a reason to think it must be slower?

Of course. I think it's fairly obvious that iterating over a  
collection and reading the values out of it is slower than simply  
adding to a variable.

Just for kicks, I performed a short test, and for/in was 7-8 times  
slower than the counter loop:

     var keyCacheLength = keyCache.length;
     for (var i = 0; i < 2000; ++i) {
         for (var j = 0; j < keyCacheLength; ++j) {
             key = keyCache[j];
         }
     }

vs.

     var keyCacheLength = keyCache.length;
     for (var i = 0; i < 2000; ++i) {
         for (j in keyCache) {
             key = keyCache[j];
         }
     }

(The keyCache array has about 200 entries.)

Regards,

   Andreas


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
qooxdoo-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to