# Hex to RGB

## JavaScript performance comparison

Revision 13 of this test case created by

## Preparation code

``````

<script>
Benchmark.prototype.setup = function() {
var data = [];
for(var i = 0; i < 10000; i++)
{
data.push(Math.floor(Math.random() * 16777215).toString(16));
}

var hexToRgb1 = function(hex) {
var bigint = parseInt(hex, 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;

return r + "," + g + "," + b;
}

var hexToRgb2 = function(hex) {
var bigint = parseInt(hex, 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;

return [r, g, b].join();
}

var hexToRgb3 = function(hex) {
return [(bigint = parseInt(hex, 16)) >> 16 & 255, bigint >> 8 & 255, bigint & 255].join();
}

var hexToRgb4 = function(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})|([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})\$/i.exec(hex);
return result ? {
r: parseInt(hex.length <= 4 ? result[4]+result[4] : result[1], 16),
g: parseInt(hex.length <= 4 ? result[5]+result[5] : result[2], 16),
b: parseInt(hex.length <= 4 ? result[6]+result[6] : result[3], 16),
toString: function() {
var arr = [];
arr.push(this.r);
arr.push(this.g);
arr.push(this.b);
return "rgb(" + arr.join(",") + ")";
}
} : null;
}

var hexToRgb5 = function(hex) {
var result = /^#?(\w\w|\w)(\w\w|\w)(\w\w|\w)\$/i.exec(hex);
if (!result) {
return null;
}
var r = parseInt(hex.length <= 4 ? result[4] + result[4] : result[1], 16);
var g = parseInt(hex.length <= 4 ? result[5] + result[5] : result[2], 16);
var b = parseInt(hex.length <= 4 ? result[6] + result[6] : result[3], 16);
return "rgb(" + r + "," + g + "," + b + ")";
}

var hexToRgb6 = function(hex) {
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])\$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});

var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})\$/i.exec(hex);
if (!result) {
return null;
}
var r = parseInt(hex.length <= 4 ? result[4] + result[4] : result[1], 16);
var g = parseInt(hex.length <= 4 ? result[5] + result[5] : result[2], 16);
var b = parseInt(hex.length <= 4 ? result[6] + result[6] : result[3], 16);
return "rgb(" + r + "," + g + "," + b + ")";
}
};

</script>
``````

## Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
hexToRgb4
``````var test;
for(var i = 0; i < data.length; i++)
{
test = hexToRgb4(data[i]);
}``````
pending…
hexToRgb6
``````var test;
for(var i = 0; i < data.length; i++)
{
test = hexToRgb6(data[i]);
}``````
pending…
hexToRgb1
``````var test;
for(var i = 0; i < data.length; i++)
{
test = hexToRgb1(data[i]);
}``````
pending…
hexToRgb5
``````var test;
for(var i = 0; i < data.length; i++)
{
test = hexToRgb5(data[i]);
}``````
pending…
hexToRgb3
``````var test;
for(var i = 0; i < data.length; i++)
{
test = hexToRgb3(data[i]);
}``````
pending…
hexToRgb2
``````var test;
for(var i = 0; i < data.length; i++)
{
test = hexToRgb2(data[i]);
}``````
pending…

## Revisions

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