Color Conversion (RGBA to INT)

JavaScript performance comparison

Test case created by Bubba

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function rgba_bitwise(r, g, b, a) {
    return (r<<24)|(g<<16)|(b<<8)|a;
  }
  function rgba_bitwise_clamp(r, g, b, a) {
    return (clamp(r)<<24)|(clamp(g)<<16)|(clamp(b)<<8)|clamp(a);
  }
  
  function rgba_math(r, g, b, a) {
    return r*16777216 + g*65536 + b*256 + a;
  }
  function rgba_math_clamp(r, g, b, a) {
    return clamp(r)*16777216 + clamp(g)*65536 + clamp(b)*256 + clamp(a);
  }
  
  function clamp(val) {
    if (val > 255) return 255;
    if (val < 0) return 0;
    return val;
  }

};
</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
Bitwise
for (var i = 0; i < 255; i++) {
  var color = rgba_bitwise(i, i, i, i);
}
pending…
Bitwise Clamped
for (var i = 0; i < 255; i++) {
  var color = rgba_bitwise_clamp(i, i, i, i);
}
pending…
Math
for (var i = 0; i < 255; i++) {
  var color = rgba_math(i, i, i, i);
}
pending…
Math Clamped
for (var i = 0; i < 255; i++) {
  var color = rgba_math_clamp(i, i, i, i);
}
pending…

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

0 Comments