Skip to main content
Participating Frequently
May 14, 2009
Question

Blacklist filter

  • May 14, 2009
  • 1 reply
  • 2024 views

I create a black list filter for a search site with this code:

<!--- blacklist function --->
<cffunction name="checkWord" returntype="boolean">
    <cfargument name="word" type="string">
    <cfset var i="">
    <cfif word is not "">
        <cfloop index="i" list="#blacklist#" delimiters="|">
            <cfif findNoCase(i,word) gt 0>
                <!--- denied word --->
                <cfreturn false>
            </cfif>
        </cfloop>
    </cfif>
    <cfreturn true>
</cffunction>

Blacklist is a string (list) with many denied word like: "porn|sex|masturb|...".

Script is not so fast, how may I optimize it?

Thanks.

Merlinox

    This topic has been closed for replies.

    1 reply

    Inspiring
    May 14, 2009

    Hi,

    You could write the function contents in one <cfscript> tag, which is heaps fasters.

    You have about 6 initial calls to CF Server (meaning each CF tag you use). When you write everything in one cfscript tag, it will become a single call to CF Server and it then executes there.

    See livedocs Extending CF with CFML scripting pages for syntax.

    Edit : I just thought of something else, do you really need to loop the list? can you not do the FindNoCase(blacklist;word) just like that? If it behaves the same, you would gain a serious performance increase...

    ex: your entire function would just be one line : <cfreturn FindNoCase(blacklist,word) GT 0>

    Cheers,

    Bert.

    merlinoxAuthor
    Participating Frequently
    May 14, 2009

    Thank you very much Bert.Dilliën! Really I program in CF from many years but really I don't know this tech note!

    I try immediately your tricks.

    Inspiring
    May 14, 2009

    I've just seen I mistyped the order of the parameters, it should be :

    <cfreturn FindNoCase(word,blacklist) GT 0 >

    but in my case, it returns 'YES' or 'NO' instead of 'true' and 'false'... weird legacy thing I suppose...