0

Following the example from: How is a JavaScript hash map implemented? I want to know whether this is the most optimized way of getting an element from an object:

var obj = {
    foo:{ hi: "higher"},
    bar:{ bye: "bye"},
    baz:{ cya: "cya"}
}

var value = obj[Object.getOwnPropertyNames(obj)[0]];
console.log(value);

I just need the most optimized way to get an element (random is fine, just need any one element in the object) from a given object, it does not matter which it is. I just need access to it and want to be able to delete it.

Is this the best implementation ?

Community
  • 1
  • 1
JerryFox
  • 615
  • 2
  • 13
  • 25
  • 1
    Well, the most optimized way would be to know the property name in advance. If you just want to get the "first" property name, using a `for...in` loop (and break after the first iteration), is probably more performant. You can always benchmark your solutions using the browser's developer tools or http://jsperf.com/ . – Felix Kling Aug 07 '14 at 19:48
  • I dont need the first just any in the object will do but I also do need to be able to delete/remove it – JerryFox Aug 07 '14 at 19:50
  • OK, then what you have works fine. Or as I said, use a `for...in` loop. Use http://jsperf.com/ to compare the performance (I assume by "optimized" you are talking about performance). – Felix Kling Aug 07 '14 at 19:52
  • I want to use the hashmap to avoid for-loops :] thanks! – JerryFox Aug 07 '14 at 19:54
  • Objects are not optimized for access without knowing the property name in advance. `Object.getOwnPropertyNames` will also iterate over the object in one way or the other and collect all property names. Use whatever works for you. If you don't want to use `for...in` loops even if they are faster, that's fine. It's all your decision, I'm just pointing out alternatives. `for...in` loops are there for iterating over objects, so they don't exclude each other, on the contrary. – Felix Kling Aug 07 '14 at 19:57
  • using any loop kind of defeats the purpose of the hashmap. If it's the case that Object.getOwnPropertyNames does loop then there must be a better implementation – JerryFox Aug 07 '14 at 19:59
  • If you don't know the property name up front the fastest way is to use a for..in loop and break on first element. `getOwnPropertyNames()` will definitely iterate over all properties, hence less performance. – Liviu Mandras Aug 07 '14 at 20:01
  • Hashmaps are great if you know the key you want to access. But it doesn't seem like you know it. So you have to iterate over the keys one way or the other. Hashmaps may just not be the optimal data structure for your use case. – Felix Kling Aug 07 '14 at 20:02

1 Answers1

1

Summarizing all the comments, your use calls for using an array of objects instead since you don't seem to be aware of the name of the name value pairs.

If you decide to use an array, push(), pop(), shift() and unshift() are available besides just removing an element depending on the index using slice() and if you intend to still go with the object route...

Then something as simple as the following should do the trick.

var object = {
    name: value,
    anotherName: anotherValue
};

var val;

for (var key in object) {
    val = key;
    break;
}

Deleting a property from an object is as simple as calling delete on it. For instance,

delete object.name;

Here is an example to illustrate. http://jsbin.com/kiwituje/1/edit

Siddharth
  • 1,146
  • 3
  • 15
  • 28
  • Given that the val variable has now has reference to the key in the object, how does one use this reference to delete it? – JerryFox Aug 07 '14 at 21:49
  • thanks! alot Btw the reason I dont use an array is to to the constant resizing my use case require if it were to use it instead of a hashmap – JerryFox Aug 07 '14 at 21:54
  • Arrays in JavaScript are dynamic. They do not require any explicit resizing. That is handled by the runtime itself. And you're welcome. – Siddharth Aug 07 '14 at 22:00
  • 1
    Im aware and that's the problem. Potentially resizing a few thousand times a second for my use case would affect performance – JerryFox Aug 07 '14 at 22:04
  • Okay. That sounds right. And object should suffice then. – Siddharth Aug 07 '14 at 22:06