Closed benjie closed 7 years ago
Thanks for your PR! I will have a look at it in the beginning of next week.
🎉 Thanks 😄
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)
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:
performance branch:
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({}); ```