for loop vs declarative style

Benchmark created on


Preparation HTML

		

Setup

const length = 7
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
const digits = '0123456789'

Test runner

Ready to run.

Testing in
TestOps/sec
for loop
let username = ''

for (let i = 0; i < length; i ++) {
  const isLetter = i === 0 ? true : Math.random() < 0.5
  const charSource = isLetter ? letters : digits
  const randomInt = Math.round(Math.random() * 100)

  username += charSource[randomInt % charSource.length]
}
ready
declarative style
Array(length).fill().map((_, i) => {
  const isLetter = i === 0 ? true : Math.random() < 0.5
  const charSource = isLetter ? letters : digits
  const randomInt = Math.round(Math.random() * 100)
  return charSource[randomInt % charSource.length]
}).join('')
ready

Revisions

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