Created
April 19, 2024 11:40
-
-
Save trycf/32ca606bab55a41801b531b15eb02416 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 = 10000; | |
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