Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6975043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:19:01+00:00 2026-05-27T17:19:01+00:00

I have a situation that I’m trying to make work with an array ,

  • 0

I have a situation that I’m trying to make work with an array, but I just don’t think it will. I want to have a list of objects, each with a unique ID, and I want to be able to easily reference a particular object without having to loop through the array searching for that ID.

If I use an object I can easily use the unique ID as the key and the object as the value. However, if I use an object instead of an array I’ll have to use a for...in loop, and I know there are issue surrounding that if someone using my code has extended Object.prototype.

So here’s my question:

Is it really that common that people extend Object.prototype that I should be weary of using it? Are there other reasons why I wouldn’t want to use a for...in loop?

On the other hand, is the performance hit of looping through an array looking for a unique ID so minimal that I shouldn’t worry about?

(For the record, the array will probably have fairly few elements in it, but I’ll be accessing it very frequently. Also, the code I’m writing will be a jQuery plugin, so I have no control over what other bad code people are combining it with.)

UPDATE:

Based on advice from @nnnnnn, I set up a jsperf test and here are the results:
http://jsperf.com/accessing-object-properties-vs-iterating-over-arrays

Basically, though the object way is slightly faster, the difference is negligible. Still, using for...in with hasOwnProperty seems cleaner.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:19:02+00:00Added an answer on May 27, 2026 at 5:19 pm

    So here’s my question:

    Is it really that common that people extend Object.prototype that I should be weary of using it? Are there other reasons why I wouldn’t want to use a for…in loop?

    No it’s not common, but it does happen. You should use a for..in loop anyway, but with .hasOwnProperty() as shown in the other answers.

    I can’t think of any reasons to not use a for..in (on an object; obviously you don’t use it on an array).

    On the other hand, is the performance hit of looping through an array looking for a unique ID so minimal that I shouldn’t worry about?

    Well you mentioned that your array won’t have many elements, but still I think that even if it performed well it would needlessly complicate your code given that you don’t have to do it at all for objects. On the other hand using .hasOwnProperty() is a minor complication that effectively becomes part of the syntax of each for..in.

    You can setup a test of the performance here: http://jsperf.com/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently in our enterprise we have a situation that i think it's not very
I have an interesting situation, well it's probably not that unique at all, but
I have this situation that when AbstractMethod method is invoked from ImplementClass I want
I have a very unusual situation that I am hoping someone will be able
I have a MVC application that I'm near completing. But I have a situation
Trying my hand with jQuery. I have this strange situation that i wish to
I have a situation that I thought would be straightforward and fairly common but
I have a situation that has been partially covered by other answers at SO,
I have a situation that requires redirecting users who are already logged in away
I have situation, where running a query that filters by an indexed column in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.