Question: I'm using an API which returns text in the following format:

```
#start
#p 09060 20131010
#p 09180 AK
#p 01001 19110212982
#end
#start
#p 09060 20131110
#p 09180 AB
#p 01001 12110212982
#end

```

I'm converting this to a list of objects:

```
var result = data.match(/#start[\s\S]+?#end/ig).map(function(v){

    var lines = v.split('\n'),
        ret = {};

    $.each(lines, function(_, v2){
        var split = v2.split(' ');
        if(split[1] && split[2]) 
            ret[split[1]] = split[2];
    });

    return ret;
});

```

My concern is that the API returns quite a lot of data, therefore I would like some feedback regarding on how to improve the performance.

For instance, is there any way to reduce the mapping complexity from O(N2) to O(N)?

Also, please suggest regex improvements :)

Answer: 
I totally agree with Uri and have some further comments:

> 
> the class has no idea what the set will contain, it does however know there maybe a IDescriptor that is meant for video..
> 
> 
> 

I'm sorry, but I have to ask: If you're not sure about exactly what the Set contains, why are you storing it in a set in the first place?

Consider instead something like `Map<KEY_TYPE, List<? extends IDescriptor>> descriptors;` where `KEY_TYPE` can be a `Class<? extends IDescriptor>`, a `String`, or something else that can tell you what type it is.

When adding items to this map you could use:

```
List<? extends IDescriptor> list = descriptors.get(descriptor.getClass());
if (list == null) {
    list = new ArrayList<>();
    descriptors.put(descriptor.getClass(), list);
}
list.add(descriptor);

```

And then all you would need to do is to use `descriptors.get(VideoDescriptor.class)` (assuming `VideoDescriptor` is not subclassed, otherwise you could loop through the supertypes as well when adding to the list).

---

Another thought is that you can use a `enum` to describe what kind of `IDescriptor` it is, and the `IDescriptor` type can have a `getDescriptorType` method. Then you could check `if (fragmentDescriptor.getDescriptorType() == DescriptorType.VIDEO)`

As I only see a portion of your code and not the big picture, I can't really tell what would be best for you.
Are you asking if you can find the first element in a list of a certain type without asking for its type? That's a strange question... What's wrong with `instanceof`?

As for the code, being code-review, I've got some observations:

This code actually finds the *last* instance of type `VideoDescriptor` in `mDescriptor`. It is also very inefficient, since it iterates over the collection **at least three times** to find it 

* It wrap it in an `ArrayList`,
* It finds *all* items of the right type (no stop condition here - so it iterates over the whole array)
* For each item of the correct type, it iterates over the array *again* using `indexOf`

(perhaps you are asking about `indexOf` and not `instanceOf`?)

A better way would be to simply iterate once over the collection, keeping the current position, returning upon reaching the correct type:

```
private int getPosition() {  
     int position = -1;

     for (IDescriptor fragmentDescriptor : mDesciptors) {
         position++;
         if (fragmentDescriptor instanceof VideoDescriptor) {
                return position;
         }
     }

     return -1;
}

```

if the collection has random access, you can use a classic `for` loop:

```
private int getPosition() {  
     for (int i = 0; i < mDescriptors.size(); i++) {
         if (mDescriptors.get(i) instanceof VideoDescriptor) {
                return i;
         }
     }

     return -1;
}

```