RGB decimal to hex (v5)

Revision 5 of this benchmark created by Nikita Vasilyev on


Description

http://github.com/DmitryBaranovskiy/raphael/blob/e3fb35b7ea5e0a270a2752beff9/raphael.js#L244-250

vs

http://twitter.com/mathias/status/21902858390 http://twitter.com/bga_/status/21903274096

vs

http://twitter.com/abozhilov/status/21906952040

Preparation HTML

<script>
  var red = 0,
      green = 150,
      blue = 255;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Raphael.js approach
var rg = /^(?=[\da-f]$)/;
var r = (~~red).toString(16).replace(rg, "0");
var g = (~~green).toString(16).replace(rg, "0");
var b = (~~blue).toString(16).replace(rg, "0");
var hex = r + g + b;
ready
@bga_'s solution
var hex = (0x1000000 | blue | (green << 8) | (red << 16)).toString(16).slice(1);
ready
@abozhilov's solution
var hex = ('000000' + ((red << 16) + (green << 8) + blue).toString(16)).slice(-6);
ready
@abozhilov's solution #2
var hex = (0x1000000 + (red << 16) + (green << 8) + blue).toString(16).slice(1);
ready

Revisions

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

  • Revision 1: published by Nikita Vasilyev on
  • Revision 5: published by Nikita Vasilyev on