Skip to content

唯一 ID 对比

Updated: at 12:22 PM

特性对比

本文以 Go 为例子,在默认配置下统计。 不包含美团 Leaf、百度 UidGenerator、滴滴 TinyId,这三个类似 Snowflake。 Sonyflake 也类似 Snowflake,但是博主比较喜欢索尼,所以在表里。

仓库地址长度字母表包含时间戳包含机器号包含随机数自定义种子自定义码表自定义长度易读性美观性URL友好排序友好分表友好输入友好操作友好可以校验
Snowflake19位[0-9]



Sonyflake18位[0-9]



UUID36位[A-Za-z0-9-]











ShortUUID22位[A-Za-z0-9]








NanoID21位[A-Za-z0-9_-]










ULID26位[0123456789ABCDEFGHJKMNPQRSTVWXYZ]




XID20位[0-9a-v]




KSUID27位[0-9A-Za-z]





Sqids6位[0-9A-Za-z]








特性解释

测试代码

package main

import (
	"fmt"
	"math/rand"
	"strconv"
	"time"

	"github.com/bwmarrin/snowflake"
	"github.com/gofrs/uuid"
	"github.com/jaevor/go-nanoid"
	"github.com/lithammer/shortuuid/v4"
	"github.com/oklog/ulid"
	"github.com/rs/xid"
	"github.com/segmentio/ksuid"
	"github.com/sony/sonyflake"
        "github.com/sqids/sqids-go"

)

func main() {
	snowflakeTest()
	sonyflakeTest()
	uuidTest()
	shortuuidTest()
	nanoidTest()
	ulidTest()
	xidTest()
	ksuidTest()
        sqidsTest()
}

func snowflakeTest() {
	n, _ := snowflake.NewNode(1)
	id := n.Generate().String()
	fmt.Println("snowflake:", id, "length:", len(id))
}

func sonyflakeTest() {
	t := time.Now()
	s := sonyflake.NewSonyflake(sonyflake.Settings{
		StartTime: t,
		MachineID: func() (uint16, error) {
			return 1, nil
		},
		CheckMachineID: func(u uint16) bool {
			return true
		},
	})
	id, _ := s.NextID()
	fmt.Println("sonyflake:", id, "length:", len(strconv.FormatUint(id, 10)))
}

func uuidTest() {
	id, _ := uuid.NewV4()
	fmt.Println("uuid:", id.String(), "length:", len(id.String()))
}

func shortuuidTest() {
	id := shortuuid.New()
	fmt.Println("shortUUID:", id, "length:", len(id))
	a := "12345#$%^&*67890qwerty/;'~!@uiopasdfghjklzxcvbnm,.()_+·><"
	id = shortuuid.NewWithAlphabet(a)
	fmt.Println("shortUuid2:", id, "length:", len(id))
}

func nanoidTest() {
	s, _ := nanoid.Standard(21)
	id := s()
	fmt.Println("nanoid:", id, "length:", len(id))
	c, _ := nanoid.CustomASCII("0123456789", 12)
	id = c()
	fmt.Println("nanoid2:", id, "length:", len(id))
}

func ulidTest() {
	t := time.Now().UTC()
	e := rand.New(rand.NewSource(t.UnixNano()))
	id := ulid.MustNew(ulid.Timestamp(t), e)
	fmt.Println("ulid:", id.String(), "length:", len(id.String()))
}

func xidTest() {
	id := xid.New()
	fmt.Println("xid:", id, "length:", len(id))
}

func ksuidTest() {
	id := ksuid.New()
	fmt.Println("ksuid:", id, "length:", len(id))
}

func sqidsTest() {
	s, _ := sqids.New()
	id, _ := s.Encode([]uint64{1, 2, 3})
        fmt.Println("sqids:", id, "length:", len(id))
        s, _ := sqids.New(sqids.Options{
		MinLength: 10,
	})
	id, _ := s.Encode([]uint64{1, 2, 3})
        fmt.Println("sqids2:", id, "length:", len(id))
	numbers := s.Decode(id)
        fmt.Println("sqids numbers:", numbers)
}