Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active January 15, 2023 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamoCA/4c70a2558ee999bf05844191dd010432 to your computer and use it in GitHub Desktop.
Save JamoCA/4c70a2558ee999bf05844191dd010432 to your computer and use it in GitHub Desktop.
CUID for Java vs CF-CUID2 Benchmarks
<cfscript>
// 2023-01-15 CUID for Java vs CF-CUID2 Benchmarks
// CUID1: 435,595 ops/sec (clcxncf7wvwy56062pd6o10xs)
// CUID2: 130,682 ops/sec (q2qimk6q6cifa0oyq3ygz316)
// CUID.isValid: 1,898,025 ops/sec (YES)
// CF-CUID2: 5,001 ops/sec (nxvr6js43lh8rtwuuv259dvi)
cuid = createobject("java", "io.github.thibaultmeyer.cuid.CUID");
// Dependency: https://github.com/thibaultmeyer/cuid-java
// JAR: https://search.maven.org/artifact/io.github.thibaultmeyer/cuid
cfcuid2 = new Cuid2();
// Dependency: https://www.bennadel.com/blog/4388-cuid2-for-coldfusion-cfml.htm
// CFC: https://github.com/bennadel/CUID2-For-ColdFusion
writeoutput("<h2>CUID for Java vs CF-CUID2 Benchmarks</h2>");
// cuid.randomCUID1
c = 0;
t = gettickcount();
while (true){
c+=1;
testVal = cuid.randomCUID1();
if (gettickcount()-t gte 1000){
break;
}
}
writeoutput("<div><b>CUID1:</b> #numberformat(c)# ops/sec (<tt>#testVal#</tt>)</div>");
// cuid.randomCUID2
c = 0;
t = gettickcount();
while (true){
c+=1;
testVal = cuid.randomCUID2();
if (gettickcount()-t gte 1000){
break;
}
}
writeoutput("<div><b>CUID2:</b> #numberformat(c)# ops/sec (<tt>#testVal#</tt>)</div>");
// cuid.isValid
c = 0;
t = gettickcount();
while (true){
c+=1;
testVal = cuid.isValid("cl9gts1kw00393647w1z4v2tc");
if (gettickcount()-t gte 1000){
break;
}
}
writeoutput("<div><b>CUID.isValid:</b> #numberformat(c)# ops/sec (<tt>#testVal#</tt>)</div>");
// CF-CUID2
c = 0;
t = gettickcount();
while (true){
c+=1;
testVal = cfcuid2.createCuid();
if (gettickcount()-t gte 1000){
break;
}
}
writeoutput("<div><b>CF-CUID2:</b> #numberformat(c)# ops/sec (<tt>#testVal#</tt>)</div>");
writeoutput("<hr>");
// cuid.randomCUID2 (1 million)
t = gettickcount();
for (i=1; i lte 1000000; i+=1){
testVal = cuid.randomCUID2();
}
writeoutput("<div><b>CUID2-million:</b> #numberformat(gettickcount()-t)# ms (<tt>#testVal#</tt>)</div>");
// CF-CUID2 (1 million)
t = gettickcount();
for (i=1; i lte 1000000; i+=1){
testVal = cfcuid2.createCuid();
}
writeoutput("<div><b>CF-CUID2-million:</b> #numberformat(gettickcount()-t)# ms (<tt>#testVal#</tt>)</div>");
</cfscript>
@bennadel
Copy link

I assume that the big difference is in the way that we are computing the SHA hash. The Java version uses the native MessageDigest class, which allows it stick to the bytes; but, I'm using the CF-native hash() function which outputs a string, which I then have to decode back into a byte array. I don't know if that's the bottleneck, but it seems like the most obvious difference to me.

I could try using the MessageDigest and see what kind of a performance differences it makes.

@bennadel
Copy link

Hmmm, just tried dropping in the MessageDigest:

var salt = generateEntropy( tokenLength );
var text = ( input & salt );


var bytes = MessageDigestClass.getInstance( "sha-256" ).digest( charsetDecode( text, "utf-8" ) );

var result = BigIntegerClass
	.init( bytes )
	.toString( 36 )
	.right( -2 )
;

return( result );

And, it's only a few seconds faster. Clearly that is not the bottleneck. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment