ColdFusion : Use arrays, lists are slower!

March 18, 2010

If you have to handle a list of data in coldfusion, you could end up using a list or an array. Well choose and array working with the data in the array is much faster compared to lists. lets do a little test for that theory:

lets define our list and array

<cfset list = “”>

<cfset array = arraynew(1)>

now lets do a loop and insert data in the list and the array

<cftimer label=”myTimerString” type=”debug”>
<cfloop from=”1″ to=”5000″ step=”1″ index=”j”>
<cfset list = listprepend(list,j)>
</cfloop>
<cfset listsort(list,”numeric”,”asc”)>
</cftimer>
<cftimer label=”myTimerArray” type=”debug”>
<cfloop from=”1″ to=”5000″ step=”1″ index=”j”>
<cfset arrayappend(array,j)>
</cfloop>
<cfset arraysort(array,”numeric”,”asc”)>
</cftimer>

so we loop from 1 to 5000 and insert the data in the list and the array surrounding with cftimer tags to see the execution times in our debug info. the results are not surprising at all. Arrays being the clear winner. Here are the results for three executions

CFTimer Times

[172ms] myTimerString

[0ms] myTimerArray

CFTimer Times

[187ms] myTimerString

[0ms] myTimerArray

CFTimer Times

[172ms] myTimerString

[0ms] myTimerArray

So when handling list data’s use arrays for your data storing and sorting. If u need to have a list at the end you can always use the arrayToList() function to get it in a list.

Interestingly in JAVA there is very little difference doing the same test. Thats a bit weird considering that coldfusion itself runs on JAVA.

peace out!


Follow

Get every new post delivered to your Inbox.