List Performance Issues in ColdFusion
Earlier this week I had the opportunity to research a slow loading function in one of our applications. I discovered the issue was related to list performance in ColdFusion and wanted to share what I found in case anyway else may be experiencing a similar issue.
I’m a big proponent of the philosophy that the way to optimize code is by making it do less work. Whenever I encounter an issue related to performance I look to see “How can I make this code do less work?”. After some investigating I isolated the problem to the following chunk of code (this a very simplified version of what I saw):
for(i = 1;i lte ListLen(someList);i++){
value = ListGetAt(someList,i);
// do something with value here
}
Immediately I saw a way to make this code do less work. The ListLen function was being used in the conditional statement for the loop. This means every time the loop was evaluated it was running this function. I immediately made the following change expecting to see my performance improve.
// put length list in variable to avoid rerunning of function in conditional statement
lengthOfList = ListLen(someList);
for(i = 1;i lte lengthOfList;i++){
value = ListGetAt(someList,i);
// do something with value here
}
Upon rerunning the function I saw an improvement in performance, but not as much as I expected. After some more debugging I determined that there was a lot of time being spent on the ListGetAt function. I began to research the management of lists in ColdFusion and found something a bit shocking… Specifically, the fact that although ColdFusion provides list functions it still just sees a list as a string under the hood. There’s no intelligence to the indexing, so when you call the function ListGetAt(list,pos) it actually traverses the string by delimiter from the beginning until it reaches the position. Though this doesn’t sound super costly, think of doing ListGetAt() in the loop we’ve been looking at. Then consider the fact that I was working with a list that contained almost 200,000 values. The cost of doing this loop is O(n^2).
// convert list to array so this loop doesn't last as long as a James Cameron movie
aConverted = ListToArray(someList);
lengthOfArray = ArrayLen(someList);
for(i = 1;i lte lengthOfArray;i++){
value = aConverted[i];
// do something with value here
}
After making this change my performance issues were resolved. I saw pages taking as much as 5-10% the amount of time the were previously taking.
Moral of the story is this, lists can be a convenient data structure in ColdFusion but be careful how you use them. If you’re going to be tracking and traversing a large amount of data then an array is going to be more efficient.
About this entry
You’re currently reading “List Performance Issues in ColdFusion,” an entry on Ricky Robinett's Blog
- Published:
- January 8, 2010 / 9:00 am
- Category:
- ColdFusion
- Tags:
- ColdFusion, ListGetAt, ListLen, Performance Issue
No comments yet
Jump to comment form | comment rss [?] | trackback uri [?]