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 ?

8
  • 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 jsperf.com . Commented Aug 7, 2014 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 Commented Aug 7, 2014 at 19:50
  • OK, then what you have works fine. Or as I said, use a for...in loop. Use jsperf.com to compare the performance (I assume by "optimized" you are talking about performance). Commented Aug 7, 2014 at 19:52
  • I want to use the hashmap to avoid for-loops :] thanks! Commented Aug 7, 2014 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. Commented Aug 7, 2014 at 19:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Given that the val variable has now has reference to the key in the object, how does one use this reference to delete it?
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
Arrays in JavaScript are dynamic. They do not require any explicit resizing. That is handled by the runtime itself. And you're welcome.
Im aware and that's the problem. Potentially resizing a few thousand times a second for my use case would affect performance
Okay. That sounds right. And object should suffice then.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.