Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.3k stars 160 forks source link

Slow #215

Open jacobg opened 10 months ago

jacobg commented 10 months ago

This library has a very nice specific purpose with a convenient function signature.

However, some feedback in comparing its speed to blueimp-load-image is that browser-image-compression is much slower, e.g., compressing a 6 MB png to 2 MB takes less than a second with blueimp-load-image, whereas it takes around 10 seconds with browser-image-compression.

For reference here is the code in blueimp-load-image to compress to a specific size:

  const image = await loadImage(file, { orientation: true })

  // We reduce by an extra 10%, because for some reason the theoretical compression factor
  // is still a little too high. Maybe there's overhead?
  const compressFactor = Math.sqrt(maxFileSize / file.size) * .9
  const canvas = await loadImage.scale(image.image, {
    maxWidth: image.originalWidth! * compressFactor,
    maxHeight: image.originalHeight! * compressFactor,
    canvas: true })

  return await new Promise<File>(resolve => {
    canvas.toBlob(
      blob => resolve(new File([blob!], file.name, { type: file.type })),
      file.type
    )
  })
qkreltms commented 3 months ago

I'm wondering did you enabled useWebWorker: true ?