Tiny utility to generate random ASCII string blazingly fast utilizing byte arrays blazingly fast!
Caveats:
- It can only generate strings with an ASCII number value of up to 255
- It utilizes
crypto.getRandomValues(min node version 15.0.0) - It can generate any length of random strings, but the larger you make them, the more explosive the memory consumption will be as it builds buffers of the requested length.
import randomString from 'fast-random-string'
randomString(10)The algorithm uses byte compiled string charsets to piece the characters byte by byte together. To generate a string with a custom charset, use the CharsetCompiler class:
import randomString, { CharsetCompiler } from 'fast-random-string'
// The string is the pool of characters. This one will generate strings with lots of "A"s.
const c = new CharsetCompiler('iuhdbfgoujhbdfgojuhjbdfgjhbAAAAAAAAAAAAA')
// You can adjust the case ('upper' | 'lower')
c.case('upper')
// or remove duplicates for balanced random strings entirely
c.unique()
// When you are done configuring, build it.
c.build()
// And use it
randomString(50, c)More compact example using from options:
// Generate a random string with a charset of only letters and numbers
randomString(1e4, CharsetCompiler.FromOptions({ letters: true, numbers: true }).case('upper').build())I have no idea what this would be useful for. It was just a fun idea I had when I was pen-testing web UI.
@ 2026 Torathion