DEV Community

Discussion on: JavaScript One-Liners That Make Me Excited

Collapse
 
lexlohr profile image
Alex Lohr

You can shave off one more char for your hex color code generator without resorting to padEnd:

// before
'#' + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, '0');

// after
'#' + (0x1000000 + Math.random() * 0xffffff).toString(16).slice(1, 6);
Collapse
 
healeycodes profile image
Andrew Healey

A clever change. Probably faster too?

Collapse
 
lexlohr profile image
Alex Lohr

Thanks. Not sure if it's faster, but that's usually a non-issue for code golfing :)

Collapse
 
rithvikvibhu profile image
Rithvik Vibhu

I was curious and created a test: jsperf.com/hex-color-code-generator
:)

Thread Thread
 
healeycodes profile image
Andrew Healey

Awesome! Close to what I expected 👍