Skip to main content

random color

stackoverflowのコードを jsbench.meでベンチマークした結果です。 (markdownの関係で、\| となっていますが、正しくは |です)

.toStringScore
'#' + (Math.random() * 0xffffff | 0).toString(16)Fastest💪
'#' + (Math.random() * (1<<24) | 0).toString(16)Fastest💪
'#' + (Math.random() * (1<<24) << 0).toString(16)+1.24%👍
'#' + parseInt(Math.random() * 0xffffff).toString(16)+2.25%👍
'#' + Math.floor(Math.random() * (1<<24)).toString(16)Fastest💪
'#' + Math.round(Math.random() * 0xffffff).toString(16)+7.51%👍
.sliceScore
'#' + Math.random().toString(16).slice(-6)+53.96%🐢
'#' + Math.random().toString(16).slice(2,8)+ 52.54%🐢
'#' + (Math.random().toString(16)+'00000').slice(2,8)+67.83%🐢
.substrScore
'#' + Math.random().toString(16).substring(-6)+50.28%🐢
'#' + (Math.random().toString(16) + "000000").substring(2,8)+67.44%🐢
'#' + ((1<<24)*(Math.random()+1) | 0).toString(16).substr(1)+15.7%👍
'#' + (((1+Math.random())*(1<<24) | 0).toString(16)).substr(-6)+15.91%👍
'#' + (0x1000000+Math.random()*0xffffff).toString(16).substr(1,6)+66.46%🐢
otherScore
'#' + Math.round((0x1000000 + 0xffffff * Math.random())).toString(16).slice(1)+20.36%👍
"#xxxxxx".replace(/x/g, y=>(Math.random()*16|0).toString(16))+85.52%🐢
rgb(${[1,2,3].map(x => Math.random()*256|0)})+69.62%🐢
hsla(${Math.random() * 360}, 100%, 50%, 1)+ 57.79%🐢
["r", "g", "b"].reduce(r => r + ("0" + ~~(Math.random()*256).toString(16)).slice(-2), "#")+89.79%🐢

REF : [1, 2, 3]