ArrayMap

Note:

You can also set the maximum thread count in ColdFusion Administrator. Click Server Settings > Settings and specify the number of threads in Default Maximum Thread Count For Parallel Functions.

Description

Iterates over every entry of the array and calls the closure function to work on the element of the array. The returned value will be set at the same index in a new array and the new array will be returned

Returns

Array

Syntax

arrayMap(array, function(item [,index, array]){} [, parallel] [, maxThreadCount])

History

ColdFusion (2021 release): Introduced the following parameters:

  • parallel
  • maxThreadCount

ColdFusion (2018 release): Introduced named parameters.

ColdFusion 11: Added this function.

Parameters

Parameter

Req/Opt

Description

array

Req

The input array.

callback

Req

Closure or a function reference that will be called for each iteration. The arguments passed to the callback are:

  • item: item in the array
  • index: index of the array
  • array: a copy of the original array. Note changes to this array will not be reflected in the original array.

maxThreadCount

Opt

The number of threads the function can execute. The number of threads must be between 1-50. If the value exceeds 50, there is an exception.

parallel

Opt

True if you want to enable parallel programming.

See also

Example

<cfscript>
 myArray=ArrayNew(1);
 myArray = [ 1,5,7,9,11 ];
 newArray = arrayMap(myArray, function(item){
       return item;
    });
 writeDump(newArray);
</cfscript>

Using parallelization

<cfscript> 
    for(i=1;i<=100001;i++){ 
        arr[i]=i 
    } 
    //writeDump(arr) 
    callback=function(item){ 
        return sqr(item) 
    } 
 
    // without parallelization 
    t_start=GetTickCount() 
    newArray=arrayMap(arr,callback) 
    t_end=GetTickCount() 
    writeOutput("Time taken without parallelization: " & t_end-t_start) 
     
    writeOutput("<br/>") 
 
    // with parallelization 
    t_start=GetTickCount() 
    pArray=arrayMap(arr,callback,true,10) 
    t_end=GetTickCount() 
    writeOutput("Time taken with 10 threads: " & t_end-t_start) 
    writeOutput("<br/>") 
 
    // with parallelization 
    t_start=GetTickCount() 
    pArray=arrayMap(arr,callback,true,20) 
    t_end=GetTickCount() 
    writeOutput("Time taken with 20 threads: " & t_end-t_start) 
    writeOutput("<br/>") 
 
    // with parallelization 
    t_start=GetTickCount() 
    pArray=arrayMap(arr,callback,true,30) 
    t_end=GetTickCount() 
    writeOutput("Time taken with 30 threads: " & t_end-t_start) 
    writeOutput("<br/>") 
 
    // with parallelization 
    t_start=GetTickCount() 
    pArray=arrayMap(arr,callback,true,40) 
    t_end=GetTickCount() 
    writeOutput("Time taken with 40 threads: " & t_end-t_start) 
    writeOutput("<br/>") 
</cfscript>
Adobe logo

Sign in to your account