Am 02.08.2006 um 10:14 schrieb Sebastian Werner:

Andreas Junghans schrieb:
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.

But we don't know. The SPEC IMHO only defines the API, not the internals.

Did you read the part I quoted? Anyway, I wrote some more tests that should convince you. In the attached perftest.html, you can try the tests labeled "Use array as array" and "Use object as array". The code for these tests is as follows:

    function useArrayAsArray() {
        var test = new Date();
        var obj = new Array(500000);
        for (var i = 0; i < 500000; ++i) {
            obj[i] = test;
        }
    }
                
    function useObjectAsArray() {
        var test = new Date();
        var obj = new Object();
        var length = 0;
        for (var i = 0; i < 500000; ++i) {
            obj[i] = test;
            ++length;
        }
    }

They perform identically in Internet Explorer, but the second method is _faster_ in Firefox. This is not really surprising since an array is just a hashmap, but it has the additional overhead of maintaining a length property.

To summarize: Internally, an array is simply a hash map (but with a little more overhead). Please read the spec and try the tests yourself if you still don't believe me. Arrays are no faster than any other object when it comes to using numeric indices - in Firefox, they're actually a little bit slower. The only features you gain by using arrays are nice methods like push and splice and an automatic length counter.

But back to whole point of this discussion: the "delete" operator is much slower than setting the value to null, so it should be avoided if possible (as Dietrich also found out).

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.)

7-8 times? Would you share you test file? I don't know that's that
dramatic. Puuh.

Test file is attached (use the tests labeled "Array with indices" and "Array with for/in"). As mentioned above, it doesn't really matter if you try this with arrays or with any other object.

Regards,

  Andreas

Temporary object creation
Prototype method call (direct)
Prototype method call (base 1)
Prototype method call (base 2)
Prototype method call (base 3)
Prototype method call (base 4)
Prototype method call (base 5)
Prototype method call (base 6)
Dictionary walk
Dictionary walk (fast)
Dictionary walk (fast, forEach)
Array walk
Array with indices
Array with for/in
Use array as array
Use object as array
in operator
get/undefined
instanceof
forEach (conventional, no extra function)
forEach (with utility function, shared iterator function)
forEach (with prototype function, shared iterator function)
forEach (with prototype function, anonymous inner iterator function)
limit (with utility function)
limit (with prototype function)
method call (in same closure)
method call (locally defined in the instance)
method call (in prototype)
string contains (conventional with indexOf)
string contains (with utility function)
string contains (with prototype function)
-------------------------------------------------------------------------
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