nano-id browser optimizations

Benchmark created by sbrichardson on


Preparation HTML

<script>
url = 'Uint8ArdomValuesObj012345679BCDEFGHIJKLMNPQRSTWXYZ_cfghkpqvwxyz-'

urlLen = url.length

</script>

Setup

var len = 64
  var random = window.crypto.getRandomValues(new Uint8Array(len))

Test runner

Ready to run.

Testing in
TestOps/sec
original nanoid-browser
let i = len,
  s = ''
    
while (0 < i--) {
  s += url[random[i] & 63]
}
ready
manual assignment
let i = len,
  s = ''
    
while (0 < i--) {
  s = s + url[random[i] & 63]
}
ready
remove < 0 comparison
let i = len,
  s = ''
    
while (i--) {
  s += url[random[i] & 63]
}
ready
combined comparison, assign
let i = len,
  s = ''
    
while (i--) {
  s = s + url[random[i] & 63]
}
ready
combined modulo, comparison, assign
let i = len,
  s = ''
    
while (i--) {
  s = s + url[random[i] % 63]
}
ready
modulo instead of bitwise &
let i = len,
  s = ''
    
while (0 < i--) {
  s += url[random[i] % 63]
}
ready
url length stored in variable
let i = len,
  s = ''
    
while (0 < i--) {
  s += url[random[i] & urlLen]
}
ready
all combined (comparison, modulo, len var, assign)
let i = len,
  s = ''
    
while (i--) {
  s = s + url[random[i] % urlLen]
}
ready
combined comparison, len var, assign
let i = len,
  s = ''
    
while (i--) {
  s = s + url[random[i] & urlLen]
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by sbrichardson on