My favorites | Sign in
Project Home Downloads Wiki Issues Code Search
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 13865: Unexpected Javascript behaviour
6 people starred this issue and may be notified of changes. Back to list
Status:  Duplicate
Merged:  issue v8:164
Owner:  ----
Closed:  Mar 2010
Cc:  ager@chromium.org, ekamensk...@chromium.org

Restricted
  • Only users with Commit permission may comment.


Sign in to add a comment
 
Reported by gvdk...@gmail.com, Jun 11, 2009
Javascript 'For ... In' loop goes through an array in a random order. This 
behaves as expected in Firefox and IE, but not in Chrome.

Run the example below in the mentioned browsers and notice the order.

I'd appreciate a reply or confirmation of receiving this report, please 
send to gvandekrol@dejong.com

Thanks and have a great day!
Guido

----------------------------------------------------
<html>
<head>
<script type="text/javascript">
<!-- 
testArray = new Array(); 
testArray[43839] = "43839"; 
testArray[21630] = "21630"; 
testArray[23985] = "23985";
testArray[42181] = "42181"; 
testArray[52262] = "52262"; 
testArray[26231] = "26231"; 
testArray[27395] = "27395"; 
testArray[42346] = "42346"; 
testArray[43007] = "43007"; 
testArray[64831] = "64831"; 
testArray[21532] = "21532"; 
testArray[48686] = "48686"; 

var x = 0; 
for (x in testArray) 
{ 
alert(x); 
} 
--> 
</script> 
</head> 
</html>
Jun 25, 2009
#1 marclall...@gmail.com
I can confirm this behavior.  It appears the the order, at least in my code, is the 
order of the associative key.  
Jul 23, 2009
#2 michal.v...@gmail.com
var x;
var mycars = new Array();
mycars[3] = "Saab";
mycars[1] = "Volvo";
mycars[0] = "BMW";

for (x in mycars)
{
document.write(mycars[x] + "<br />");
}

IE, FF, Safari sort items by the order when the item was added into array (Saab, 
VOlvo, BMW)

however Chrome and other minor browsers sorts items by their index (BMW, Volvo, Saab)

Therefore there can be iregularities on website, while processing for example some 
JSON data and items are displayed in different order.

Or another example: {"0":"BMW","10000000":"Volvo","100":"Saab"} - how to iterate it 
(without knowing indexes) and keep item order? Regex? :D



Jul 23, 2009
#3 gvdk...@gmail.com
var x;
testArray = new Array(); 
testArray[43839] = "43839"; 
testArray[21630] = "21630"; 
testArray[23985] = "23985";
testArray[42181] = "42181"; 
testArray[52262] = "52262"; 
testArray[26231] = "26231"; 
testArray[27395] = "27395"; 
testArray[42346] = "42346"; 
testArray[43007] = "43007"; 
testArray[64831] = "64831"; 
testArray[21532] = "21532"; 
testArray[48686] = "48686"; 
for (x in testArray)
{
document.write(testArray[x] + "<br />");
}

This is the order in IE, Firefox:
43839
21630
23985
42181
52262
26231
27395
42346
43007
64831
21532
48686

This is Chrome:
23985
27395
43839
21532
48686
52262
43007
26231
64831
42181
21630
42346

For Chrome, I really don't see an order. It seems just random, not order of associate 
key.
Jul 23, 2009
#4 marclall...@gmail.com
Chrome development is taking the hard stance that an associative array (which is what
you have defined) has no defined order, and iteration over that array will have no
defined order.  

While, technically, I agree with them, the fact of the matter is that oodles of code
out in the real world assume that iterating over an associative array will be ordered
in the order of insertion.

Personally, I think they should make changes to conform to the other major browsers,
but they seem to feel differently.
Jul 23, 2009
#5 gvdk...@gmail.com
Are they trying to prove a point by randomizing the order, it would seem that it would 
take more effort than just using the simpler order of insertion.. 
Jul 23, 2009
#6 marclall...@gmail.com
No, of course not.  I don't know how they've implemented the associative array, but
the method they used doesn't automatically store them in the order entered.  This
makes sense and most associative that I'm aware of use key hashing for quick retrieval.

If I were writing an associative array system that also needed to support iteration
in some predefined order (like order of entry), then I would probably need to
maintain an independent index outside of the array that ordered the elements.

Chrome development, in another thread, claimed that this could slow Chrome down.  One
of the more prolific testers called BS, saying that other JS engines that were
already much faster seemed to manage without any problem.
Jul 23, 2009
#7 gvdk...@gmail.com
Okay I hope they will look at it from a practical standpoint. I would think that most 
scripts want to be aware of the order of their array, so people will still have either 
to sort the array or find some way to keep track of the order of insertion in Chrome. 
(multidimensional array?). So by performing these action you still lose speed even if 
Chrome itself can be faster by not keeping track of the independent index.
Jul 23, 2009
#8 marclall...@gmail.com
Considering that one of the major uses of this sort of thing, where the lack of a
defined order gets you the most is a <select> tag, I agree with you and hope they
change it.

Otherwise we'll all be making ordered arrays of key/value pair.  What a pain.
Jan 19, 2010
#9 phajdan.jr@chromium.org
(No comment was entered for this change.)
Labels: -Area-Misc Area-WebKit WebKit-JavaScript
Feb 16, 2010
#10 ka...@chromium.org
(No comment was entered for this change.)
Labels: Mstone-X kglater
Feb 26, 2010
#11 jon.ronn...@gmail.com
This has been an issue since version 2.0. Please fix!
Feb 26, 2010
#12 jon.ronn...@gmail.com
I should point out that this bug is not in Safari and I've never seen it in that 
browser. Suggesting that this is not a webkit bug but a V8 bug.
Mar 3, 2010
#13 ekamensk...@chromium.org
Executing javascript from comment #3 for the latest Chromium version 5.0.343.0
(40501), I get:

21532
21630
23985
26231
27395
42181
42346
43007
43839
48686
52262
64831

As said, FF gives: 

43839
21630
23985
42181
52262
26231
27395
42346
43007
64831
21532
48686

Cc: a...@chromium.org ekamensk...@chromium.org
Mar 3, 2010
#14 dglazkov@chromium.org
(No comment was entered for this change.)
Status: Duplicate
Mergedinto: v8:164
Oct 12, 2012
#15 bugdro...@chromium.org
This issue has been closed for some time. No one will pay attention to new comments.
If you are seeing this bug or have new data, please click New Issue to start a new bug.
Labels: Restrict-AddIssueComment-Commit
Mergedinto: v8:164
Mar 10, 2013
#16 bugdro...@chromium.org
(No comment was entered for this change.)
Labels: -Area-WebKit -WebKit-JavaScript Cr-Content-JavaScript Cr-Content
Apr 5, 2013
#17 bugdro...@chromium.org
(No comment was entered for this change.)
Labels: -Cr-Content Cr-Blink
Apr 5, 2013
#18 bugdro...@chromium.org
(No comment was entered for this change.)
Labels: -Cr-Content-JavaScript Cr-Blink-JavaScript
Sign in to add a comment

Powered by Google Project Hosting