Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does it compare to Salesforce UUID? #8

Open
yippie opened this issue Jul 24, 2024 · 3 comments
Open

How does it compare to Salesforce UUID? #8

yippie opened this issue Jul 24, 2024 · 3 comments

Comments

@yippie
Copy link

yippie commented Jul 24, 2024

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

@jongpie
Copy link
Owner

jongpie commented Jul 24, 2024

@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!

@yippie
Copy link
Author

yippie commented Jul 24, 2024

@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.

@jongpie
Copy link
Owner

jongpie commented Jul 30, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants