ApexUUID icon indicating copy to clipboard operation
ApexUUID copied to clipboard

How does it compare to Salesforce UUID?

Open yippie opened this issue 1 year ago • 3 comments

Salesforce just released their own UUID class. Does that make this project redundant or are there still advantages or extras here?

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_UUID.htm

yippie avatar Jul 24 '24 11:07 yippie

@yippie great question - the short answer is that the Salesforce class System.UUID is so much faster, this project is now redundant. I am planning to add a note to the project's README file, and to archive this project.

The loooooong answer is.....

Both options provide the exact same thing

There are multiple versions of the UUID standard, but both this project and Salesforce's class System.UUID generate a v4 random UUID. So you get the same functionality with both options.

  • I had planned to implement other versions of the UUID standard in this project (especially v6 & v7 because they can be sorted in a meaningful way), but I unfortunately don't have the time to do so. I also have not heard of many people needing support for other versions of UUID, so it doesn't seem worth it at this time to work on implementing other versions.

System.UUID is the most performant option

The System.UUID class is incredibly fast, and much faster than this project's code can ever perform. A few months ago, I did some benchmarking to compare 3 options for generating a unique ID in Apex:

  1. ULID in the ApexKit repo - https://github.com/codefriar/ApexKit/blob/3a6be83941e6da807dba02bab48543fc331363f1/force-app/main/default/classes/ULID/ULID.cls
  2. This project (Apex UUID)
  3. Summer 24's new standard Apex class System.UUID - https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_uuid.htm&release=248&type=5"

To test out these options, I ran this Apex test class ~10 times & calculated the average runtime for each option.

@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));
    }
}

The end result is that the System.UUID is the fastest option, by far.

image

I've stopped using Apex UUID

I original started this project because I needed a UUID for some functionality in Nebula Logger, and I've used it there for several years. But because System.UUID is so much faster (and provides the same thing), I switched Nebula Logger to using System.UUID in the v4.13.0 release

Hope that covers everything, but let me know if you have any other questions!

jongpie avatar Jul 24 '24 16:07 jongpie

@jongpie WOW what an incredible response. I never would have guessed there would be that big of a performance difference. Thank you! People like you make the community great.

I had expected no response and mostly posted the question as a warning for people to look at the System one now. At best I was hoping for someone to say they do the same thing.

yippie avatar Jul 24 '24 17:07 yippie

@yippie happy to help! I learned a lot from working on this repo years ago, but it always felt like a gap that the platform itself didn't have a way to generate a UUID. And seeing the performance difference of the System class, it seems like the winning option at this point.

Thanks again for asking this question! I'm working on adding some of these details to README and then I'll be archiving this repo.

jongpie avatar Jul 30 '24 14:07 jongpie