Comments (2)
This implementation is not string-compatible with typical big-int based implementation. Saying Base62 encoding and decoding, it is correct, it encodes arbitrary bytes to string using 62 characters, then correctly decodes the string back to the original bytes. The correctness is tested by large amount of random bytes, see the test file https://github.com/jxskiss/base62/blob/master/base62_test.go.
This implemention is much performant than typical big-int based implementation. Regarding the reversed order of bytes, it's an implementation detail, and is the main reason we can get the performance.
The folling test code gives this:
(anaconda3-2018.12) ➜ temp git:(master) ✗ go test -count=1 ./testbase62
ok temp/testbase62 0.011s
(anaconda3-2018.12) ➜ temp git:(master) ✗ go test -run=none -bench=. ./testbase62
goos: darwin
goarch: amd64
pkg: temp/testbase62
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Benchmark_jxskiss_Base62_1K-12 95632 11263 ns/op
Benchmark_saltpack_Base62_1K-12 13999 87196 ns/op
Benchmark_jxskiss_Base62_4K-12 25191 44248 ns/op
Benchmark_saltpack_Base62_4K-12 3537 347388 ns/op
Benchmark_jxskiss_Base62_1M-12 100 11347393 ns/op
Benchmark_saltpack_Base62_1M-12 13 88337949 ns/op
PASS
ok temp/testbase62 8.553s
package testbase62
import (
"bytes"
"crypto/rand"
"testing"
"github.com/jxskiss/base62"
"github.com/keybase/saltpack/encoding/basex"
)
var bytes1K []byte
var bytes4K []byte
var bytes1M []byte
func init() {
bytes1K = make([]byte, 1024)
bytes4K = make([]byte, 4096)
bytes1M = make([]byte, 1024*1024)
_, err1 := rand.Read(bytes1K)
_, err2 := rand.Read(bytes4K)
_, err3 := rand.Read(bytes1M)
if err1 != nil || err2 != nil || err3 != nil {
panic("rand.Read got error")
}
}
func TestCorrectness(t *testing.T) {
b1 := base62.Encode(bytes4K)
b2, err := base62.Decode(b1)
if err != nil {
panic("base62 error")
}
if !bytes.Equal(bytes4K, b2) {
panic("base62 not equal")
}
n3 := basex.Base62StdEncoding.EncodedLen(len(bytes4K))
b3 := make([]byte, n3)
basex.Base62StdEncoding.Encode(b3, bytes4K)
n4 := basex.Base62StdEncoding.DecodedLen(len(b3))
b4 := make([]byte, n4)
nDec, err := basex.Base62StdEncoding.Decode(b4, b3)
if err != nil {
panic("basex error")
}
b4 = b4[:nDec]
if !bytes.Equal(bytes4K, b4) {
panic("basex not equal")
}
}
func Benchmark_jxskiss_Base62_1K(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = base62.Encode(bytes1K)
}
}
func Benchmark_saltpack_Base62_1K(b *testing.B) {
for i := 0; i < b.N; i++ {
n := basex.Base62StdEncoding.EncodedLen(len(bytes1K))
buf := make([]byte, n)
basex.Base62StdEncoding.Encode(buf, bytes1K)
}
}
func Benchmark_jxskiss_Base62_4K(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = base62.Encode(bytes4K)
}
}
func Benchmark_saltpack_Base62_4K(b *testing.B) {
for i := 0; i < b.N; i++ {
n := basex.Base62StdEncoding.EncodedLen(len(bytes4K))
buf := make([]byte, n)
basex.Base62StdEncoding.Encode(buf, bytes4K)
}
}
func Benchmark_jxskiss_Base62_1M(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = base62.Encode(bytes1M)
}
}
func Benchmark_saltpack_Base62_1M(b *testing.B) {
for i := 0; i < b.N; i++ {
n := basex.Base62StdEncoding.EncodedLen(len(bytes1M))
buf := make([]byte, n)
basex.Base62StdEncoding.Encode(buf, bytes1M)
}
}
from base62.
Hope I will get some time later to add the implementation details to README.
from base62.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from base62.