nanoid.cr
Crystal implementation of original NanoID
Installation
Add this to your application's shard.yml
:
dependencies:
nanoid:
github: mamantoha/nanoid.cr
Usage
require "nanoid"
Nanoid.generate
uses URL-friendly symbols (A-Za-z0-9_-
) and returns an ID with 21 characters.
Nanoid.generate
# => 3gFI8yZxcfXsXGhB0036l
If you want to reduce ID length (and increase collisions probability), you can pass the length as an argument.
Nanoid.generate(size: 8, alphabet: "1234567890abcdef")
# => 86984b57
Non-secure API is also available.
Nanoid.generate(secure: false)
# => no0MmiInNcBm9jR2_3sGt
Nanoid.generate(size: 10, alphabet: "1234567890abcdef", secure: false)
# => 2793b2351c
Benchmark
crystal bench/benchmark.cr --release
Crystal 0.34.0-dev [c4d70bc4e] (2020-03-01)
LLVM: 8.0.0
Default target: x86_64-unknown-linux-gnu
Nanoid.simple_generate(21) 596.08m ( 1.68s ) (± 9.72%) 244MB/op 2.50× slower
Nanoid.complex_generate(21) 517.15m ( 1.93s ) (± 8.54%) 259MB/op 2.88× slower
Nanoid.non_secure_generate(21) 1.16 (859.21ms) (±22.61%) 214MB/op 1.28× slower
UUID v4 1.49 (671.57ms) (± 3.73%) 0.0B/op fastest
Nanoid.simple_generate(16) 815.82m ( 1.23s ) (±10.06%) 229MB/op 1.85× slower
Nanoid.complex_generate(16) 612.54m ( 1.63s ) (± 8.70%) 241MB/op 2.46× slower
Nanoid.non_secure_generate(16) 1.50 (665.54ms) (±13.96%) 198MB/op 1.00× slower
UUID 1.51 (664.34ms) (± 1.81%) 0.0B/op fastest
Development
Clone this repository and install dependencies:
shards install
Run tests:
crystal spec
Contributing
- Fork it (https://github.com/mamantoha/nanoid.cr/fork)
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- mamantoha Anton Maminov - creator, maintainer
License
Copyright: 2018-2020 Anton Maminov ([email protected])
This library is distributed under the MIT license. Please see the LICENSE file.