Created
January 30, 2024 00:23
-
-
Save jongpie/d65bba24bc9c882cacc3c040479bfc7d to your computer and use it in GitHub Desktop.
Apex - Unique ID Generation Benchmarks
This file contains hidden or 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
@IsTest | |
private class UniqueIdBenchmarkingTests { | |
@IsTest | |
static void ulidBenchmark() { | |
Long ulidStartTime = System.now().getTime(); | |
System.debug('ULID generation start: ' + System.now().getTime()); | |
for (Integer i = 0; i < 1000; i++) { | |
ULID.generate(); | |
} | |
Long ulidStopTime = System.now().getTime(); | |
System.debug('ULID generation finished: ' + ulidStopTime + ', total time: ' + (ulidStopTime - ulidStartTime)); | |
} | |
@IsTest | |
static void customUUIDBenchmark() { | |
Long customUUIDStartTime = System.now().getTime(); | |
System.debug('Custom UUID generation start: ' + System.now().getTime()); | |
for (Integer i = 0; i < 1000; i++) { | |
new UUID().getValue(); | |
} | |
Long customUUIDStopTime = System.now().getTime(); | |
System.debug('Custom UUID generation finished: ' + customUUIDStopTime + ', total time: ' + (customUUIDStopTime - customUUIDStartTime)); | |
} | |
@IsTest | |
static void systemUUIDBenchmark() { | |
Long systemUUIDStartTime = System.now().getTime(); | |
System.debug('System.UUID generation start: ' + systemUUIDStartTime); | |
for (Integer i = 0; i < 1000; i++) { | |
System.UUID.randomUUID(); | |
} | |
Long systemUUIDStopTime = System.now().getTime(); | |
System.debug('System.UUID generation finished: ' + systemUUIDStopTime + ', total time: ' + (systemUUIDStopTime - systemUUIDStartTime)); | |
} | |
} |
Link to Google Drive Sheet with results of the benchmarks - https://docs.google.com/spreadsheets/d/1Elk_Ox8sN-yX3xBarg8NmMEkwwCKLOcRwPWB0hyYil0/edit?usp=drive_web&ouid=114299458270522861990
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This Apex test class compares the performance of unique ID generation, using 3 options
ULID
in the ApexKit repo - https://github.com/codefriar/ApexKit/blob/3a6be83941e6da807dba02bab48543fc331363f1/force-app/main/default/classes/ULID/ULID.clsUUID
in the ApexUUID repo - https://github.com/jongpie/ApexUUID/blob/2a008ccaa80663beddd3a2fa38bbe31b724f0f71/apex-uuid/classes/Uuid.clsSystem.UUID
- https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_uuid.htm&release=248&type=5"