Lasse said:
There is no casting anywhere. In Javascript, arrays *are* objects, and
their numbered properties are just that: object properties. The only
"magic" in arrays are in the internal [[put]] method, which keeps
the "length" property in sync with the numbered properties. A literal
implementation of the ECMAScript specification of objects would use
the same [[get]] method for arrays and non-array objects.
Sorry but that's an urban legend greatly dispropaganded in the 3rd
edition of my <
http://www.geocities.com/schools_ring/ArrayAndHash.html>
and it will be completely busted in the 4th one I'm hoping to finish
before Christmas. To facilitate the cultural shock some people may
experience
here a piece of tast cases directly related to the
current OP. You are welcome to put each position into your own
benchmark test.
All comments are inside.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Test Template</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script type="text/javascript">
var obj = {};
var arr = [];
function init() {
}
function test() {
/**
* Each block below should be uncommented
* separately before testing and commented
* back before moving to the next block
*/
/**
* Both Hashtable (used for Object) and Array
* accept non-ECMA naming compliant literals as property name:
*/
//obj["^^^CDATA^^^"] = "foo";
//arr["^^^CDATA^^^"] = "foo";
//alert(obj["^^^CDATA^^^"]); // "foo"
//alert(arr["^^^CDATA^^^"]); // "foo"
/**
* Array neither counts its properties
* as array elements nor it knows anything
* about them:
*/
//alert(arr.length); // 0
/**
* But Array's Object envelope
* knows about them:
*/
//alert("^^^CDATA^^^" in arr); // true
/**
* Array can hold up to 4294967295 indexed elements
* (which is 32 bit unsigned minus one bit for the length flag).
* This means that the maximum index you can use in array is
* 4294967294
*/
//arr[4294967294] = "foobar";
//alert(arr.length); // 4294967294
/**
* An integer bigger than 4294967294 or lesser than 0
* (so it cannot be used as an array index) turns on
* JavaScript Baby Care mechanics. Instead of simply break the
execution
* with "Invalid array index value" message as any other language would
do,
* interpreter switches the object from Array-mode to Hashtable-mode,
* converts argument from number to string and adds new key to the
hash.
* Naturally array doesn't know about it, exactly as Dr. Jekyll had
nothing
* but to guess what Mr. Hyde this time did.
* Also valuable cycles are being spent for all this marry-go-round.
*/
//arr[4294967294 + 1] = "foobar";
//alert(arr.length); // 0
//alert(4294967295 in arr) // true
/**
* The same as above happens if argument number but not an integer.
* Also valuable cycles are being spent for all this marry-go-round.
*/
//arr[1.5] = "bar";
//alert(arr.length); // 0
/**
* Serving a string index to array turns on
* JavaScript Baby Care mechanics also. First interpreter
* attempts to convert the string into valid index value.
* If it's possible: then the situation gets really ambigious:
* hell does user want to add "33" property or array[33] ?!?
* To cover all holes, interpreter both add new array element [33]
* and new property "33".
* Needless to say that a create amount of cycles goes for this
* thinking and arrangement.
*/
//arr["33"] = "foo";
//alert(arr.length); // 34
//alert("33" in arr) // true
/**
* If string value is not convertable into a valid array index,
* interpreter simply switches object from Array-mode to
* Object-mode and adds new property to the Object envelope.
* Still cycles are being vasted unless you really wanted to add
* new property to your array.
*/
arr["foo"] = "bar";
alert(arr.length); // 0
alert("foo" in arr) // true
/**
* These are all very basics above you need to understand
* clearly before to deal with code benchmarks: otherwise
* they will be one huge unresolved mistery to you.
* And naturally JavaScript Baby Care has absolutely nothing
* to do with Hashtable or Array nature, same way as its typeless
* has nothing to do with String or Number nature.
*/
}
function testIE() {
test();
}
function testFF() {
test();
}
window.onload = init;
</script>
<style type="text/css">
body {background-color: #FFFFFF}
var {font: normal 10pt Verdana, Geneva, sans-serif;
color: #0000FF; text-decoration: underline;
cursor: hand; cursor
ointer}
</style>
</head>
<body>
<noscript>
<p><font color="#FF0000"><b>JavaScript disabled:-</b><br>
<i><u>italized links</u></i> will not work properly</font></p>
</noscript>
<!--[if gte IE 5]>
<p>
<var onclick="testIE()">Test</var>
</p>
<![endif]-->
<![if ! gte IE 5]>
<p>
<var onclick="testFF()">Test</var>
</p>
<![endif]>
</body>
</html>