Comment #95 on issue 164 by atcrabtree: Wrong order in Object properties
interation
http://code.google.com/p/v8/issues/detail?id=164
I'd just like to add for all the frustration of current developers that a
great deal of this annoyance can be overcome by simply accepting reality. I
agree that ordering should be maintained to allow for true ordered
associative-arrays-like objects. However, ordered associative arrays do not
exist in JavaScript like in PHP or other languages.
What I have done in production on a regular basis is to use an array for
ordering (the proper implementation):
var res = [{id: 27, name: 'foo'}, {id: 12, name: 'bar'}, {id: 33,
name: 'baz'}];
and then use a generated id lookup map:
var map = {27: 0, 12: 1, 33: 2};
Results can then be looped in order:
for (var i in res) {console.log(i,res[i]);}
or looked up as an associative array:
console.log(map[27],res[map[27]]);
This requires minimal overhead in the form of a single additional hash, and
can easily be generated by looping through the res object just once.
The status of this bug annoys me and I wish it was fixed, but some of the
JS devs' complaints about it being "broken" or cumbersome to work around
are a bit dramatic considering the simplicity of the workaround. I'd still
prefer if ordering was maintained however, as the speed gain does seem to
be somewhat synthetic, though who can really speak on behalf of all
existing devs' implementations.
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev