I want to create an object, starting from something like:
var map = {};
Then, I want to add items with this function:
add = function(integerA, objectB) {
map[objectB.type][integerA] = objectB;
}
So, this is a random example of the object structure I want to achieve:
map = {
'SomeType' : { 0 : 'obj', 2 : 'obj', 3 : 'obj' },
'OtherType' : { 0 : 'obj', 5 : 'obj' },
};
Now, my problem. I can't do map[objectB.type][integerA] = objectB;
because map[objectB.type]
is not defined. I could solve this by checking if map[objectB.type]
exists through an if-statement and create map[objectB.type] = {};
when necessary.
Otherwise I could pre-load all object types. However I would prefer not to have to do this.
My question: is there a way I can create the object 'on the fly' without having to check if the type already exists every time I want to call the add
function or to pre-load all the types?
It is important that my add function is so fast as possible and that the map object is correct, because I need to read and write a lot in a small amount of time (it's an animation / game application).
I want to create an object, starting from something like:
var map = {};
Then, I want to add items with this function:
add = function(integerA, objectB) {
map[objectB.type][integerA] = objectB;
}
So, this is a random example of the object structure I want to achieve:
map = {
'SomeType' : { 0 : 'obj', 2 : 'obj', 3 : 'obj' },
'OtherType' : { 0 : 'obj', 5 : 'obj' },
};
Now, my problem. I can't do map[objectB.type][integerA] = objectB;
because map[objectB.type]
is not defined. I could solve this by checking if map[objectB.type]
exists through an if-statement and create map[objectB.type] = {};
when necessary.
Otherwise I could pre-load all object types. However I would prefer not to have to do this.
My question: is there a way I can create the object 'on the fly' without having to check if the type already exists every time I want to call the add
function or to pre-load all the types?
It is important that my add function is so fast as possible and that the map object is correct, because I need to read and write a lot in a small amount of time (it's an animation / game application).
Share Improve this question asked Jul 10, 2011 at 20:13 user8363user8363 1,4094 gold badges14 silver badges17 bronze badges 2- 1 are you sure it will be a performance problem? if I were you, i'd try it first and see if its a problem. at the end of the day, either you will need to do the check, or the javascript engine, so the instructions will need to be processed anyway – Ant Kutschera Commented Jul 10, 2011 at 20:17
- this link has the answer for ur question stackoverflow.com/questions/7744611/… – Dark Prince Commented Sep 23, 2013 at 12:01
3 Answers
Reset to default 9No, there is no any other way to create objects on the fly. Only check for existence every time:
add = function(integerA, objectB) {
if (!map[objectB.type]) {
map[objectB.type] = {};
}
map[objectB.type][integerA] = objectB;
}
If you want to improve performance you might consider some caching technics.
You can use the boolean OR shortcut (which avoids at least an explicit if
). It might not be that readable though:
var data = map[objectB.type] || (map[objectB.type] = {});
data[integerA] = objectB;
This works because an assignment actually returns the value that was assigned and an OR expression returns the first value that evaluates to true
.
I don't think using an if
has any impact on the performance though (actually, the way in my answer might be even "slower").
If you use the map only for lookups and you don't need to iterate over the dimensions, you could merge your dimensions into a single key. For example:
add = function(integerA, objectB) {
var key = objectB.type + '-' + integerA;
map[key] = objectB;
}