constrgbToHex=(...rgb)=>'#'+rgb.reduce((a,v)=>a+(256|v).toString(16).slice(1),'')constrgbToHex=(...rgb)=>'#'+rgb.reduce((a,v)=>a+v.toString(16).padStart(2,0),'')constrandomHex=(a='facedb')=>[...a].reduce(x=>x+(a+0x3d00b615)[~~(Math.random()*16)],'#')constrandomHex=(f='reduce')=>[...f][f](a=>a+(~~(Math.random()*16)).toString(16),'#')constrandomHex=()=>'#'+(~~(Math.random()*(1<<24))|1<<25).toString(16).slice(1)constrandomHex=()=>'#'+(~~(Math.random()*(1<<24))).toString(16).padStart(6,0)constrandomHex=()=>'#'+(~~(Math.random()*8**8)).toString(16).padStart(6,0)// Anyone got any shorter?constreverse=str=>[...str].reduce((a,v)=>v+a)constisEven=x=>~x&1constcapitalize=([a,...b])=>a.toUpperCase()+b.join('')constcapitalize=a=>a.replace(/./,a=>a.toUpperCase())
I made no claim to it being faster, but in this case the reduce method is almost twice as fast - jsbench.me/3tkttupth4/1 - at least on Chrome. On Firefox though, the situation is reversed. Different JS engines, different optimisations I guess
😊
I made no claim to it being faster, but in this case the
reduce
method is almost twice as fast - jsbench.me/3tkttupth4/1 - at least on Chrome. On Firefox though, the situation is reversed. Different JS engines, different optimisations I guessFaster !== Correct ... his string reverse breaks with code points, while
[...str]
doesn't.Try it: OP reverse breaks the emoji with
reverse('some 💩');
while this reduce suggestion doesn't.P.S.
In a bid to be obnoxious 😏 you could have shortened your code further by 2 characters 🌚
But who has the need in the real world / real apps to reverse a string? Nobody.
alternative reverse:
:D