Skip to content

Commit 696aab1

Browse files
committed
Remove array destructuring in colorDelta
I'm looking to improve performance and I observed that if we avoid array destructuring in this function, we can make my performance test that uses this function heavily go from ~10s to ~6.5s. This is surprising to me, but the tradeoff here is pretty obvious.
1 parent 98eb1d4 commit 696aab1

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/colorDelta.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ function isFillerPixel([r, g, b, a]) {
2727
//
2828
// Modified from https://github.com/mapbox/pixelmatch
2929
module.exports = function colorDelta(previousPixel, currentPixel) {
30-
let [r1, g1, b1, a1] = previousPixel;
31-
let [r2, g2, b2, a2] = currentPixel;
30+
// We are not using array destructuring because it is significantly slower,
31+
// and we are sensitive to performance here.
32+
let r1 = previousPixel[0];
33+
let g1 = previousPixel[1];
34+
let b1 = previousPixel[2];
35+
let a1 = previousPixel[3];
36+
let r2 = currentPixel[0];
37+
let g2 = currentPixel[1];
38+
let b2 = currentPixel[2];
39+
let a2 = currentPixel[3];
3240

3341
if (r1 === r2 && g1 === g2 && b1 === b2 && a1 === a2) {
3442
return 0;

0 commit comments

Comments
 (0)