1

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();
        }
    }
2
  • I think you have a logic error in your Hashtable code. The dateFields[key] = new Date(); should be inside an "else" condition, right? Commented Jul 11, 2016 at 22:54
  • No, it's correct because anything that is NaN is going to get a new date which is the current date. I can understand that maybe it would be overwritten, but I didn't think about it before but it worked anyway. Commented Jul 12, 2016 at 2:30

1 Answer 1

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...)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.