Created
April 19, 2024 11:39
-
-
Save trycf/b7d434dd5b5f72a2083fadd2da307cff to your computer and use it in GitHub Desktop.
TryCF Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
public array function queryToArrayOfStructs( required any q ) { | |
try { | |
var queryColumns = getMetadata( arguments.q ).map( function( item ) { | |
return item.name; | |
} ); | |
} catch ( any e ) { | |
writeDump( var = arguments ); | |
rethrow; | |
} | |
return queryReduce( | |
arguments.q, | |
function( results, row ) { | |
var rowData = structNew( "ordered" ); | |
for ( var column in queryColumns ) { | |
rowData[ column ] = row[ column ]; | |
} | |
results.append( rowData ); | |
return results; | |
}, [] | |
); | |
} | |
public array function queryToArrayOfStructsRefactor( required any q ) { | |
try { | |
var queryColumns = getMetadata( arguments.q ).map( function( item ) { | |
return item.name; | |
} ); | |
} catch ( any e ) { | |
writeDump( var = arguments ); | |
rethrow; | |
} | |
var results = []; | |
if ( arguments.q.recordCount > 0 ) { | |
arrayResize( results, arguments.q.recordCount ); | |
} | |
for ( var row in arguments.q ) { | |
var rowData = structNew( "ordered" ); | |
for ( var column in queryColumns ) { | |
rowData[ column ] = row[ column ]; | |
} | |
results[ arguments.q.currentRow ] = rowData; | |
} | |
return results; | |
} | |
c = 1000; | |
n = 0; | |
seed = []; | |
while( n++ < c ) { | |
arrayAppend( seed, { ID: n } ); | |
} | |
q = QueryNew("id", "integer", seed); | |
s = getTickCount(); | |
a = queryToArrayOfStructs(q); | |
e = getTickCount(); | |
writeoutput("<hr>original #e-s# ms #arrayLen(a)#"); | |
s = getTickCount(); | |
a = queryToArrayOfStructsRefactor(q); | |
e = getTickCount(); | |
writeoutput("<hr>refactored #e-s# ms #arrayLen(a)#"); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment