Comparing these libraries
- github.com/google/uuid
- github.com/lucsky/cuid
- github.com/matoous/go-nanoid
$ go version
go version go1.16 darwin/arm64
$ sysctl machdep.cpu.brand_string
machdep.cpu.brand_string: Apple M1
$ go test -bench . -benchmem -benchtime 4s ./
goos: darwin
goarch: arm64
pkg: github.com/evalphobia/benchmark-uuid
BenchmarkUUID-8 43347363 108.5 ns/op 64 B/op 2 allocs/op
BenchmarkCUIDNew-8 22307738 212.0 ns/op 55 B/op 4 allocs/op
BenchmarkCUIDSlug-8 25321903 198.3 ns/op 32 B/op 3 allocs/op
BenchmarkCUIDNewCrypto-8 10721586 455.2 ns/op 168 B/op 12 allocs/op
BenchmarkNanoID-8 19495198 238.7 ns/op 144 B/op 3 allocs/op
BenchmarkHashids-8 10606057 460.7 ns/op 512 B/op 5 allocs/op
BenchmarkSHA256-8 24044833 202.2 ns/op 120 B/op 3 allocs/op
PASS
ok github.com/evalphobia/benchmark-uuid 57.397s
Size: 100M ids
go test -v -count 1 -timeout 1h
=== RUN TestCollisionUUID
collision_test.go:23: [uuid.NewString()] No collision occurs through #[100000000] iterations
collision_test.go:37: [uuid.NewString()] Example output => []string{"23a16872-5718-4de6-928a-64374df60096", "92333847-e968-490a-bd17-a4041f3ad5b8", "68a4ff5f-572a-4318-bdcc-bcd84479c5a2", "4a35d4e2-3f7f-430d-8c0b-ee2bec1e2e14", "fa40d5e7-ab0d-40fc-9837-22df6bfb22f0"}
--- PASS: TestCollisionUUID (77.34s)
=== RUN TestCollisionCUIDNew
collision_test.go:23: [cuid.New()] No collision occurs through #[100000000] iterations
collision_test.go:37: [cuid.New()] Example output => []string{"ckosap7d1g9fn85962zsgiw2l", "ckosapg2e66hr85965u9il26t", "ckosaplnuhgfa8596rspq9tip", "ckosapn4q5az68596idtfl3e0", "ckosapw2re4vt8596f3jr0fjq"}
--- PASS: TestCollisionCUIDNew (84.97s)
=== RUN TestCollisionCUIDSlug
collision_test.go:23: [cuid.Slug()] No collision occurs through #[100000000] iterations
collision_test.go:37: [cuid.Slug()] Example output => []string{"jzaug86zlbu", "pm81lg864i8f", "ha790e867p53", "henq6w86isy3", "egp9o986r"}
--- PASS: TestCollisionCUIDSlug (70.30s)
=== RUN TestCollisionCUIDNewCrypto
collision_test.go:23: [cuid.NewCrypto(rand.Reader)] No collision occurs through #[100000000] iterations
collision_test.go:37: [cuid.NewCrypto(rand.Reader)] Example output => []string{"ckosat9fjg8k28596uv0c3eoy", "ckosashut5jbi8596t2zyrh18", "ckosastvxhb7p8596zogh6eam", "ckosasuondhkz8596oyvgu9sf", "ckosau0ivp9zd8596ha0jku55"}
--- PASS: TestCollisionCUIDNewCrypto (116.19s)
=== RUN TestCollisionNanoID
collision_test.go:23: [gonanoid.New()] No collision occurs through #[100000000] iterations
collision_test.go:37: [gonanoid.New()] Example output => []string{"e71VKEGTiY4b86IR62avd", "NuRANiaeikRFUusp9MNrs", "dkeHoPrezI1wZAMncQYAV", "nqKbsho7CVdokQfX4kiG3", "O7Nx5Y39BtyzToIUIWAkT"}
--- PASS: TestCollisionNanoID (108.15s)
=== RUN TestCollisionHashids
collision_test.go:23: [hashids.New()] No collision occurs through #[100000000] iterations
collision_test.go:37: [hashids.New()] Example output => []string{"Y6A4MW", "007E23", "OPVNNR", "jMrZ9y", "64llKV"}
--- PASS: TestCollisionHashids (107.42s)
=== RUN TestCollisionSHA256
collision_test.go:23: [sha256.Sum256()] No collision occurs through #[100000000] iterations
collision_test.go:37: [sha256.Sum256()] Example output => []string{"Ld/skjHfqpC8pzJ5/dZgFgsYS6fdIJ6kfDaXidZZhdk=", "SbtcUnTAhyfQeCY3ThJ7wftHb5R5wHFZdQzrUJFgPvc=", "NtNRpLtbAwGB0YLzBBPROeupYR0oEco5b9LDYexYB4M=", "kluoZksvasIlxfrQv63SajnIGBFb91UhNqf3uEJKASE=", "BVaDIoBRF1GiKEHfKVSNb3UDcbX1GdebQSOR/xAtfCU="}
--- PASS: TestCollisionSHA256 (183.74s)
PASS
ok github.com/evalphobia/benchmark-uuid 749.048s
MacBook Pro (13-inch, M1, 2020)
- OS: macOS 11.2.3
- CPU: Apple M1
- Memory: 16GB