function pixelsDiff(imgA, imgB, threshold = 0) {
  if (imgA.width !== imgB.width || imgA.height !== imgB.height) {
    throw new Error('Images must have the same size');
  }

  let differentPixels = 0;
  const numColorParams = 4; // RGBA

  for (let pixelIndex = 0; pixelIndex < imgA.data.length; pixelIndex += numColorParams) {
    // Get pixel's color for each image.
    const [aR, aG, aB] = imgA.data.subarray(pixelIndex, pixelIndex + numColorParams);
    const [bR, bG, bB] = imgB.data.subarray(pixelIndex, pixelIndex + numColorParams);

    // Get average pixel's color for each image (make them greyscale).
    const aAvgColor = Math.floor((aR + aG + aB) / 3);
    const bAvgColor = Math.floor((bR + bG + bB) / 3);

    // Compare pixel colors.
    if (Math.abs(aAvgColor - bAvgColor) > threshold) {
      differentPixels += 1;
    }
  }

  return differentPixels;
}
Compares two images and finds the number of different pixels.

@param {ImageData} imgA - ImageData for the first image.
@param {ImageData} imgB - ImageData for the second image.
@param {number} threshold - Color difference threshold [0..255]. Smaller - stricter.
@returns {number} - Number of different pixels.