I had been looking into many examples of Hashtable vs Array and in every case, an example of a Hashtable winning is posted and a justification for using an array instead is a bit more ambiguous, but so far no solid example of an array actually outperforming a hashtable so it would be great if someone could provide one.
I do have a side-request if anyone cares to look at this code in which I'm not sure if the performance of the array is any different than that of the hashtable. I'm no experienced programmer but from the looks of this, I can only guess that there must be barely any difference but even if it's minimal I would like to know which one worked better:
Array:
var dateFields = [FechaEnvio, FechaInicio, Vencimiento]; //In case if any of the date fields are erased on purpose, current date will be set instead.
for(var i = 0; i < dateFields.length; i++) {
var date = new Date(dateFields[i]);
if(isNaN(date.getDate())) {
if(i == dateFields.length - 1) {
var newDate = new Date(dateFields[i - 1]); //Vencimiento is automatically set as one year from FechaInicio
newDate.setYear(newDate.getFullYear() + 1);
dateFields[i] = newDate;
} else
dateFields[i] = new Date();
}
Hashtable
var dateFields = new Object(); //In case if any of the date fields are erased on purpose, current date will be set instead.
dateFields['FechaEnvio'] = FechaEnvio;
dateFields['FechaInicio'] = FechaInicio;
dateFields['Vencimiento'] = Vencimiento;
for(key in dateFields) {
if(isNaN(dateFields[key])) {
if(key == 'Vencimiento') {
var newDate = new Date(dateFields['FechaInicio']); //Vencimiento is automatically set as one year from FechaInicio
newDate.setYear(newDate.getFullYear() + 1);
dateFields[key] = newDate;
} else
dateFields[key] = new Date();
}
}
答案 0 :(得分:2)
This kind of question is extremely specific to which EMCAScript engine is being used. According to all the official documentation, an Array is just an object with a magic length
property. Behind the scenes, all those indexes are really just string values and you can add non-numeric properties (though it can get weird later.)
Thus -- unless that particular JavaScript engine has some kind of special code to speed up array access, the performance results should be almost identical.
(This is assuming a standard filled array. Sparce arrays
are a special case...)