Improve frame colors worker perfomance by caching

This commit is contained in:
Dávid Szabó 2016-08-25 20:16:02 +02:00 committed by Julian Descottes
parent 712262b82a
commit f829d589d5

View File

@ -34,13 +34,24 @@
} }
}; };
var intHexCache = {};
var intToHex = function(int) {
if (intHexCache[int]) {
return intHexCache[int];
}
var hex = rgbToHex(int & 0xff, int >> 8 & 0xff, int >> 16 & 0xff);
intHexCache[int] = hex;
return hex;
};
var getFrameColors = function (frame) { var getFrameColors = function (frame) {
var frameColors = {}; var frameColors = {};
var transparentColorInt = 0; // TODO: Fix magic number var transparentColorInt = 0; // TODO: Fix magic number
for (var i = 0; i < frame.length; i++) { for (var i = 0; i < frame.length; i++) {
var color = frame[i]; var color = frame[i];
if (color !== transparentColorInt) { if (color !== transparentColorInt) {
var hexColor = rgbToHex(color & 0xff, color >> 16 & 0xff, color >> 8 & 0xff); var hexColor = intToHex(color);
frameColors[hexColor] = true; frameColors[hexColor] = true;
} }
} }