javascript - Few large arrays, or one large array of few variables? -
apologies if question has been asked before i'm finding hard word question in way might have been asked before.
q: more efficient have like: mass[128] = {0.0} speed[128] = {0.0} age[128] = {0}
or: properties[128] = {mass=0.0, speed=0.0, age=0}
and why? there simple rule bear in mind, (are few larger arrays better many small etc)?
i'm writing in js using chrome. reading , writing elements often.
thanks much!
in general, answer here is: makes sense let write simplest, clearest code; , worry performance or memory issue if run one.
using array of objects named properties more efficient in terms of access time on modern javascript engine, , less efficient in terms of memory use. in both cases, difference incredibly minor , imperceptible.
if values numbers , arrays can of fixed size, might use typed arrays, since arrays (where normal arrays aren't1 unless javascript engine can optimization). there downsides typed arrays (their being fixed size, instance), again, if , when becomes necessary...
example of array of objects named properties:
var properties = [ {mass: 0, speed: 0, age: 0}, {mass: 1, speed: 1, age: 1}, // ... ];
if you're using es2015 (you said you're using chrome, can), might make const
:
const properties = [ {mass: 0, speed: 0, age: 0}, {mass: 1, speed: 1, age: 1}, // ... ];
that only makes properties
constant, not contents of array points to, can still add, remove, or amend entries desired.
1 that's post on anemic little blog.
Comments
Post a Comment