rgbtohex

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

<script>
  var red = 0,
      green = 150,
      blue = 145.67;
 
  var toHexString_garrett = function (r, g, b) {
    return (toHexString = _toHexString)(r,g,b);
    function _toHexString(r, g, b) {
      return "#" + toHexByte(r) + toHexByte(g) + toHexByte(b);
    }
    function toHexByte(bite) {
      var hex = bite.toString(16);
      return (hex.length === 2 ? hex : "0" + hex);
    }
  };
 
  function toHexString_as(a,b,c){
    return"#"+((256+a<<8|b)<<8|c).toString(16).slice(1);
  }
 
  function toHexString_jed(a,b,c){
    return"#"+((256+a<<8|b)<<8|c).toString(16).slice(1);
  }
 
  function toHexString_asen(r,g,b) {
    return ("00000" + (r << 16 | g << 8 | b).toString(16)).slice(-6);
  }
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Raphael
var r = (~~red).toString(16)
var g = (~~green).toString(16)
var b = (~~blue).toString(16)
var hex = r + g + b;
pending…
2
hex = '#' + (0x1000000 | blue | (green << 8) | (red << 16)).toString(16).slice(1);
pending…
Garrett's
hex = toHexString_garrett(red, green, blue);
pending…
atesgoral's
var hex = toHexString_as(red, green, blue);
pending…
Jed's
var hex = toHexString_jed(red, green, blue);
pending…
Asen's
var hex = toHexString_asen(red, green, blue);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment