I’m building an Entity System in Javascript. It’s basically a technique of composing in-world objects out of components, like “Position”, or “Sprite”. These components only hold data, and they have only minor methods.
I’m implementing it in a sort of database, the entity is just an id for getting to instances of components.
The “database” is a dictionary, that will look like this:
{
"ComponentType": ComponentInstances
}
A Component instance is accesed like this: db[componentType][entityId].
I’m concerned as to what data type would be the most efficient for for entityId: a string or a number? How much memory do these types take in Javascript (I know it varies, but even an estimation would be useful)?
Of course, if it was a string, the database would be implemented like a dictionary-of-dictionaries, and if it was a number, a dictionary-of-arrays (which doesn’t matter because both are objects).
Edit: compared to a reference, how much memory heavy are strings and numbers? Are they more efficient?
While conceptually both numbers and strings are treated the same when used as keys (keys are always treated as strings in Javascript – x[0] and x[“0”] return the same value), most recent implementations will special case the numeric keys and store them in a contiguous array.
Therefore, I would go for contiguous numeric indexes if I were you.