Skip to main content
Known Participant
March 15, 2019
Question

Difference between arrayContains() vs listContains()?

  • March 15, 2019
  • 1 reply
  • 455 views

Hello everyone, I'm wondering if anyone can tell me pros/cons of either of these two functions? Which one is preferred and if there is any difference in performance/security, etc? I used arrayContains() thinking it's better/faster for validations some fields on the back end as well as reducing number of lines in situations like this:

<cfif userAccess eq 1>

       <!--- Do something --->

</cfif>

<cfif userAccess eq 4>

      <!--- Do something --->

</cfif>

Instead I used this:

<cfif arrayContains([1,4],userAccess)>

     <!--- Do something --->

</cfif>

​Would listFind() or listContains() ​be better fit in this case or not? Please if anyone have information about the differences let me know. Thank you.

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
March 17, 2019

I would go by feel on this one. I would use, by default, if userAccess eq 1. I would go to the trouble of creating a userAccessList or userAccessArray only if there are more than a handful of userAccess values, say, more than 5. I would then do something like arrayContains(userAccessArray,userAccess).

The choice between  userAccessList and userAccessArray will depend very much on which one I will be able to reuse elsewhere in the code. After all, you gain performance not at one place but in the software as a whole.