# Searching through Arrays (most efficient method)

**URL:** <https://forum.kirupa.com/t/searching-through-arrays-most-efficient-method/308373>\
**Category:** flash\
**Created:** [April 21, 2010, 6:58pm UTC](https://forum.kirupa.com/t/searching-through-arrays-most-efficient-method/308373 "2010-04-21T18:58:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![IQAndreas](https://avatars.discourse-cdn.com/v4/letter/i/f08c70/32.png) [@IQAndreas](https://forum.kirupa.com/u/IQAndreas)\
**Post date:** [April 21, 2010, 6:58pm UTC](https://forum.kirupa.com/t/searching-through-arrays-most-efficient-method/308373/1 "2010-04-21T18:58:46Z")

</div>

_To save people from reading too much, this is the “abbreviated” version, while the next post will contain the “full version” in it’s original glory._

Here is the deal. I have a “User” class with 4 properties, all Strings. I want to store those instances inside of a “UserList” class. The UserList class has **several properties to sort through and find users**.

Now, at least the “getUserByPrefix” and “getUserByNickname” require to **retrieve values very quickly** , so (I’m guessing) that options like this are\*\* not a good idea\*\*:

```auto
function getUserByPrefix(prefix:String):User
{
   for (var i:int = 0; i < userArray; i++)
   {
      if (userArray*.prefix == prefix) 
         { return userArray*; }
   }
}

```

**So far, I can think of one workaround** , storing an additional Object (basically an array with strings as keys) for quick retrieval. This works since I know the **“prefix” value will not change**.

```auto
var userArray:Array = new Array();
var prefixArray:Object = {};
function addUser(user:User):void
{
   userArray.push(user);
   prefixArray[user.prefix] = user;
}

function getUserByPrefix(prefix:String):User
{
   return prefixArray[prefix];
}

```

The problem is, **for each UserList instance there may be 4 or 5 arrays with values. If there are 30 UserList instances, that means there are 150 arrays each containing maybe 100 User references in them**. Also, this system won’t work in case properties such as “prefix” change over time.

This probably isn’t too bad compared to how bad it could be.

**Are there any more ways of doing this efficiently?**
