0

I'm developing web XMPP chat. After retrieving of Roster I'm putting people infos to array of objects, but before pushing object to array I must check for it previous existence on that array, and it's quite slow, because it makes operations in count of sum of arithmetic series, which is ex. 820 for 40 number of contacts, and that number doesn't make me happy. And my question is: are there any optimizations possible for search method in this case?

  1. Also I don't wan't to convert array to associative array.
  2. Objects are not sorted.

I know there are other threads, but there is nothing about optimization.

4
  • 1
    Sorting and using an object are the two ways, why don't you want to do that? Commented May 18, 2015 at 17:49
  • Sorting will take a lot of time, if I'll use object(associative array) i also need to convert it to array anyway, to make it renderable by my gui js framework. Commented May 18, 2015 at 17:52
  • So what's wrong with managing an object AND array? Commented May 18, 2015 at 17:52
  • What am I missing? it sounds like he is just searching an array of 40 contacts... which should be very, very quick, right? Commented May 18, 2015 at 17:54

1 Answer 1

1

Use both an array an an object.

var info_array = [];
var info_object = {};

Then when you get a new info, do:

if (!info_object[info.name]) {
    info_array.push(info);
    info_object[info.name] = info;
}

Now you can either iterate over info_array, or access them by name quickly using info_object.

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

2 Comments

You gave me another idea, and now, it will be fastest as possible, without using much of memory.
If you came up with a better solution, you should post it as an answer.

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.