spacer spacer spacer


Flash Hash

The new Flash 7 beta demonstrates a significant performance improvement.

Presumably some of that improvement comes from improving an algorithm that must be central to any Javascript virtual machine: looking up Object members. Objects in Javascript can have members with any Javascript string as the member name. Most implementations will use a hash table to implement Objects, where the hash key is the member name and the hash value is the value corresponding to that member.

Arrays in Javascript are really just Objects that happen to have members with names like '0', '1', '2', etc., and a special length property that is always 1 more than the member with the highest numeric index.

Implementing Array’s efficiently must be tricky, because programs will typically ask for array members using numeric indices, but Javascript semantics state that they must behave as if the numeric index was converted to a string first and then used to look up the member. Furthermore, you are allowed to add members to an array with non-numeric indices, so you can’t specialize your hash table to only support numeric indices.

Clearly, you’d like to avoid the overhead of converting to a string if you can. One way I can think of doing that would be to have a clever hash algorithm that hashes numbers and the string representation of a number to the same value, then you can delay the number-to-string conversion until you have a key to compare to.

I’m thinking that Macromedia may have an optimization something like that in the Flash 7 player, because of the following bug that I have found. If I set any array to have an element at '-', it seems to corrupt all the other arrays in my application that have an element at 0 — they all behave as if their 0 element moved to '-'.

If you have Flash 7 beta installed, you can see for yourself: Flash 7 Bug

Here’s the source code for this demonstration:

mylist.addItem("Create an empty array: var crud = new Array;");
var crud = new Array;
mylist.addItem("Store a value at '-':  crud['-'] = 'foo'");
crud['-'] = 'foo';
mylist.addItem("Create an array of one element: var ary = [ 'one' ];");
var ary = [ 'one' ];
mylist.addItem("What is at ary[0] now?");
var j = '0';
mylist.addItem('ary[' + j + '] => [' + typeof(ary[j]) + ' ' + ary[j] + ']');
mylist.addItem("What is at ary['-'] now?");
j = '-';
mylist.addItem('ary[' + j + '] => [' + typeof(ary[j]) + ' ' + ary[j] + ']');
mylist.addItem("What are all the elements of ary?");
for (var i in ary) {
	mylist.addItem('ary[' + i + '] => [' + typeof(ary[i]) + ' ' + ary[i] + ']');

The latest release of the Flash 7 player (7.0r2) fixes this bug. Thanks!

2003-08-25 12:31 | P T Withington

Post a comment

Thanks for signing in, . Now you can comment. (sign out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Remember me?

You are not signed in. You need to be registered to comment on this site. Sign in