markusn / color-diff

Implemets the CIEDE2000 color difference algorithm, conversion between RGB and lab color and mapping all colors in palette X to the closest color in palette Y based on the CIEDE2000 difference.
Other
359 stars 30 forks source link

Performance improvements #13

Closed benjie closed 7 years ago

benjie commented 7 years ago

Hoisting the functions to the outer scope increases performance by about 20%. The other tweaks move the needle by the tiniest amount, but in the right direction.

master branch:

rnd.gen 2.9628049787171404 ± 0.14880101230839793
colorDiff.closest 181.64721476923273 ± 7.260007105342422
colorDiff.closest [predef] 171.09526993905783 ± 7.195477461087955

performance branch:

rnd.gen 2.973347308582935 ± 0.20252592561612162
colorDiff.closest 155.08481424744338 ± 5.489338215168882
colorDiff.closest [predef] 143.9483535199002 ± 3.7500997878289497
Click to reveal the benchmark code ```js const Benchmark = require('benchmark'); const colorDiff = require('./lib'); const suite = new Benchmark.Suite; const rnd = function () { return { R: Math.floor(Math.random() * 256), G: Math.floor(Math.random() * 256), B: Math.floor(Math.random() * 256), }; }; const rndA = function () { return [ rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), rnd(), ]; }; const predefTarget = rnd(); const predefPalette = rndA(); suite .add('rnd.gen', function () { return [rnd(), rndA()]; }) .add('colorDiff.closest', function () { return colorDiff.closest(rnd(), rndA()); }) .add('colorDiff.closest [predef]', function () { return colorDiff.closest(predefTarget, predefPalette); }) .on('complete', function () { for (let i = 0; i < 3; i += 1) { console.log(this[i].name, this[i].stats.mean * 1000000, '±', this[i].stats.deviation * 1000000); } }) .run({}); ```
coveralls commented 7 years ago

Coverage Status

Coverage remained the same at 99.329% when pulling 318c37fa63cf11afebccb39c087adf55e0f44b12 on benjie:performance into ad7d2da284546078abeee52dbf5fe3fc421f31ac on markusn:master.

coveralls commented 7 years ago

Coverage Status

Coverage remained the same at 99.329% when pulling 21f5b8e8e9e065c96faa8a872f71c7fa55f92231 on benjie:performance into ad7d2da284546078abeee52dbf5fe3fc421f31ac on markusn:master.

markusn commented 7 years ago

Thanks for your PR! I will have a look at it in the beginning of next week.

benjie commented 7 years ago

🎉 Thanks 😄

markusn commented 7 years ago

Thank you! Let me know if you need a tagged version, otherwise I will postpone tagging until the rest of your PRs have been handled (will become 1.1)