0

I'm writing a program that essentially stores a series of scenarios. These scenarios need to be pulled one at a time, and only once, in a random order. I'm using a Fisher-Yates shuffle to shuffle the array, but I'm curious as to the best method to use to store the individual aspects of each scenario. Currently, I'm using numbers and just calculating which scenario corresponds to each number. (For example, if it were cards, each suit would be assigned a number zero through 3, and a card's number in the array would be it's card number (1-13) + (13 * suit)).

I don't think this is a very good choice, and I want to exchange it by using either a multidimensional array to store the data, or an array of objects. Which would be better / more efficient?

2
  • Unless you have millions or billions of these, it doesn't matter.
    – Jon
    Jan 20, 2012 at 15:36
  • I know, but I'm curious as to the answer for much larger amounts of data.
    – CSturgess
    Jan 20, 2012 at 15:38

1 Answer 1

3

JavaScript arrays aren't really arrays in the classic sense of a contiguous block of memory you index into using a numeric index that gets multiplied by the size of an entry; they're just objects with a bit of special behavior around a class of property names (ones that are all digits — e.g., "indexes") and the length property (plus they have the Array.prototype backing them, and so array instances have things like slice and such). (For details, see the article linked above or Section 15.4 of the specification.)

So if you don't need the special handling around array "indexes," just use an object.

1
  • @jAndy: Yes, there is a move toward adding true arrays. So far only Firefox and Chrome (not sure about Safari). Jan 20, 2012 at 15:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.