Prefer using arrays instead of Object types to represent ordered collections
The ECMAScript standard does not specify the storage order of properties in JavaScript's Object type.
But when using the for..in loop to traverse the properties in Object, you do need to rely on a certain order. Precisely because ECMAScript does not explicitly standardize this sequence, each JavaScript execution engine can be implemented according to its own characteristics, so the behavioral consistency of the for..in loop cannot be guaranteed in different execution environments.
For example, the result of the following code when calling the report method is uncertain:
function report(highScores) { var result = ""; var i = 1; for (var name in highScores) { // unpredictable order result += i + ". " + name + ": " + highScores[name] + "\n"; i++; } return result; } report([{ name: "Hank", points: 1110100 }, { name: "Steve", points: 1064500 }, { name: "Billy", points: 1050200 }]); // ?
If you really need to ensure that the running result is based on the order of the data, use the array type first. Represent data instead of using the Object type directly. At the same time, try to avoid using for..in loops and use explicit for loops:
function report(highScores) { var result = ""; for (var i = 0, n = highScores.length; i < n; i++) { var score = highScores[i]; result += (i + 1) + ". " + score.name + ": " + score.points + "\n"; } return result; } report([{ name: "Hank", points: 1110100 }, { name: "Steve", points: 1064500 }, { name: "Billy", points: 1050200 }]); // "1. Hank: 1110100 2. Steve: 1064500 3. Billy: 1050200\n"
Another behavior that is particularly dependent on order is the calculation of floating point numbers:
var ratings = { "Good Will Hunting": 0.8, "Mystic River": 0.7, "21": 0.6, "Doubt": 0.9 };
In Item 2, it is mentioned that the addition operation of floating point numbers cannot even satisfy the commutative law: The results of
(0.1 + 0.2) + 0.3 and the results of 0.1 + (0.2 + 0.3) are
0.600000000000001 and 0.6
So for arithmetic operations on floating point numbers, arbitrary order cannot be used:
var total = 0, count = 0; for (var key in ratings) { // unpredictable order total += ratings[key]; count++; } total /= count; total; // ?
When the traversal order of for..in is different, the final total result will be different, as follows are the two calculation orders and their corresponding results:
(0.8 + 0.7 + 0.6 +0.9) / 4 // 0.75 (0.6 + 0.8 + 0.7 +0.9) / 4 // 0.7499999999999999
Of course, for problems such as calculation of floating point numbers, one solution is to use integers to represent them. For example, we first put the above floating point numbers Magnify it 10 times into integer data, and then reduce it 10 times after the calculation is completed:
(8+ 7 + 6 + 9) / 4 / 10 // 0.75 (6+ 8 + 7 + 9) / 4 / 10 // 0.75
The above is the detailed content of Why is array preferred in javascript's cyclic ordered collection?. For more information, please follow other related articles on the PHP Chinese website!