# Efficiently finding the index of a target record

**URL:** <https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492>\
**Category:** Classic Servoy\
**Created:** [July 8, 2005, 1:57pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492 "2005-07-08T13:57:25Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morley](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Morley](https://forum.servoy.com/u/Morley)\
**Post date:** [July 8, 2005, 1:57pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/1 "2005-07-08T13:57:25Z")

</div>

On arriving on a form I want to find the current index for a record (not necessarily the current record) with a particular ID. Since the record count is usually no more than 50 I’ve been doing this via looping, comparing the current record with the target. However performance is very bad.

I am reliably told it’s because I’m using the getMaxRecordIndex() command just ahead of the loop. Is there a technique for identifying the target record’s current index without looping?

---

<div class="post-metadata">

**Author:** ![swingman](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/swingman/32/4536_2.png) [@swingman](https://forum.servoy.com/u/swingman)\
**Post date:** [July 8, 2005, 2:15pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/2 "2005-07-08T14:15:48Z")

</div>

Just an idea:

What about getting the pk’s into an array using the databasemanager, and looping through the array to look for your record?

---

<div class="post-metadata">

**Author:** ![Morley](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Morley](https://forum.servoy.com/u/Morley)\
**Post date:** [July 8, 2005, 2:35pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/3 "2005-07-08T14:35:35Z")

</div>

Not performance tested but I think I may have found a solution to my own problem.

```auto
var size = foundset.getSize();
for ( var i = 1 ; i <= size; i++ )
{
	var record = foundset.getRecord(i);
	if ( globals.gcrid == record.crid )
	{
		break;
	}
}
controller.setSelectedIndex(i);

```

---

<div class="post-metadata">

**Author:** ![david](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@david](https://forum.servoy.com/u/david)\
**Post date:** [July 11, 2005, 9:34pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/4 "2005-07-11T21:34:02Z")

</div>

So “foundset.getSize()” is faster than “getMaxRecordIndex()”? Interesting.

---

<div class="post-metadata">

**Author:** ![Morley](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Morley](https://forum.servoy.com/u/Morley)\
**Post date:** [July 12, 2005, 3:20am UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/5 "2005-07-12T03:20:27Z")

</div>

> david:  
> So “foundset.getSize()” is faster than “getMaxRecordIndex()”? Interesting.

You yourself pointed out by phone how expensive getMaxRecordIndex can be in terms of performance. Switching to foundset.getSize() has indeed cut times dramatically.

In many cases the two commands produce identical results.

I’m not sure but I suspect getMaxRecordIndex() attempts to load all possible records to perform its calculation, while foundset.getSize() just returns the record count of the current set, all that’s really wanted most of the time. I’m gropping around for an explanation of the two architectures, but something along those lines is probably at work.

Perhaps Jan Blok or Maarten can elucidate.

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [July 12, 2005, 8:04am UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/6 "2005-07-12T08:04:45Z")

</div>

As far as I see that, those two are the same…

---

<div class="post-metadata">

**Author:** ![maarten](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@maarten](https://forum.servoy.com/u/maarten)\
**Post date:** [July 12, 2005, 11:49am UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/7 "2005-07-12T11:49:59Z")

</div>

looping through the foundset Object is generally faster cause foundset doesn’t update the GUI after each record loop

This can be illustrated by placing “myField” in a table/list view  
and then running these 2 scripts:

```auto
for(var i=1 ; i<= foundset.getSize() ; i++)
{
var record = foundset.getRecord(i)
record.myField = "abc"
}

```

```auto
for(var i=1 ; i<= controller.getMaxRecordIndex() ; i++)
{
controller.setSelectedIndex(i)
myField="def"
}

```

The foundset was added in later versions of Servoy  
in order to boost performance when looping through recordsets.

So try to stick with foundset Object when doing loops.

---

<div class="post-metadata">

**Author:** ![Morley](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Morley](https://forum.servoy.com/u/Morley)\
**Post date:** [July 12, 2005, 1:15pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/8 "2005-07-12T13:15:01Z")

</div>

There are circumstances in which I want to process ALL available records, to keep loading, not just process the current set. Am I correct in assuming only the following will do that?

```auto
for(var i=1 ; i<= controller.getMaxRecordIndex() ; i++)
{
controller.setSelectedIndex(i)
myField="def"
}

```

---

<div class="post-metadata">

**Author:** ![maarten](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@maarten](https://forum.servoy.com/u/maarten)\
**Post date:** [July 12, 2005, 1:33pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/9 "2005-07-12T13:33:49Z")

</div>

it will loop through your foundset, NOT all records in your table

example  
assume you’ve done a search with result of 1000 records (5000 rec total in table)  
var counter = foundset.getSize()  
// counter will have value of about 200 (currently present in memory)  
so next loop will only go through part of your foundset

```auto
for(var i=1 ; i<=counter ; i++)
{
//only loops 200 times
}

```

To make sure you go through the whole foundset do:

```auto
for (var i=1 ; i<= foundset.getSize() ; i++)
{
// the value of "foundset.getSize()" increases until end of foundset is reached. (1000)
}

```

If you want to loop through all records in table  
first do controller.loadAllRecords() before stsrting loop

---

<div class="post-metadata">

**Author:** ![Morley](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@Morley](https://forum.servoy.com/u/Morley)\
**Post date:** [July 12, 2005, 2:39pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/10 "2005-07-12T14:39:38Z")

</div>

Thanks. That clarifies things considerably.

It would be helpful to have advice along those lines in the docs for these functions.

---

<div class="post-metadata">

**Author:** ![maarten](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@maarten](https://forum.servoy.com/u/maarten)\
**Post date:** [July 12, 2005, 2:47pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/11 "2005-07-12T14:47:06Z")

</div>

You’re right.  
I will ask Marc Norman to put this example in the docs.

---

<div class="post-metadata">

**Author:** ![ebrandt](https://avatars.discourse-cdn.com/v4/letter/e/96bed5/32.png) [@ebrandt](https://forum.servoy.com/u/ebrandt)\
**Post date:** [July 12, 2005, 6:18pm UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/12 "2005-07-12T18:18:31Z")

</div>

I have a question along these lines

> for (var i=1 ; i\<= foundset.getSize() ; i++)  
> {  
> // the value of “foundset.getSize()” increases until end of foundset is reached. (1000)

Will a relation based counter only loop through 200 records?

for (var i=1 ; i\<= orders\_to\_lineitems.getsize() ; i++)  
{  
}

Thanks,  
Erich

---

<div class="post-metadata">

**Author:** ![maarten](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@maarten](https://forum.servoy.com/u/maarten)\
**Post date:** [July 14, 2005, 10:00am UTC](https://forum.servoy.com/t/efficiently-finding-the-index-of-a-target-record/4492/13 "2005-07-14T10:00:46Z")

</div>

no, same behaviour as foundset.getSize()
