简体   繁体   中英

nodejs choosing between JIMP and MOZJPEG

I was wondering if there is a blaring reason to use vs. for compressing jpegs (I am already using both imagemin and jimp in my project already, imagemin-webp to serve next gen images, and jimp to convert pngs to jpegs in rare cases) So I am more looking for reasoning that is based on the following:来压缩 jpeg(我已经在我的项目中同时使用 imagemin 和 jimp,imagemin-webp 用于提供下一代图像,而 jimp 将 png 转换为 jpeg在极少数情况下)所以我更多地寻找基于以下内容的推理:

  1. Performance
  2. Reliability (I have noticed that there are some JPEGs mozjpeg has trouble with and fails on. specifically ones that I have used GNU Image Manipulation Program [GIMP] with.)

However, if someone has good reasons that don't align with the two aforementioned I would still like to hear them.

Heres some quick links to the NPM packages mentioned if anyone needs them:
imagemin-mozjpeg
jimp

Performance

imagemin-mozjpeg uses mozjpeg to process images. And mozjpeg itself is made using C language. While jimp uses javascript to process it.

As mention in the main repository jimp :

An image processing library for Node written entirely in JavaScript, with zero native dependencies.

We know the difference in performance between Javascript and C.

Reliability

I do not want much opinion in this section. but we can see directly how the statistics of each repository.

mozjpeg :

  • Star: 4.1k
  • Open Issues: 76
  • Closed Issues: 186

jimp :

  • Star: 10.3k
  • Open Issues: 157
  • Closed Issues: 430

I do not side with either. They all have worked well. I really appreciate the work of the maintainers and contributors of the library library.

Yes, and it goes far beyond the performance of the compression process (ie how long it takes to compress an image, which is also important) or the relative activity of development of the library (which is arguably less important).

I highly recommend reading Is WebP really better than JPEG? (and this discussion ), which shows that even among JPEG compression libraries, the implementation can have a significant impact on compression ratio.

In short, MozJPEG produces jpeg files that are 10% smaller than jpeg files produced by the reference JPEG implementation (libjpeg). Even more interesting, for images larger than 500px, MozJPEG actually produces jpeg files that are smaller than WebP.

This leads to an interesting question. It will depend on exactly your use case and priorities, but it might actually make sense to simplify and use MozJPEG for everything, and ditch WebP entirely.

Looking forward, AVIF might make sense as a true next-gen format (delivering 30% smaller images), but browser support is "coming soon". Alternatively, JPEG XL also looks promising, but the standard hasn't been finalized yet. HEIC is problematic and I wouldn't count on wide support.


Warning regarding jimp:

As jimp is implemented in pure JavaScript, all image operations end up blocking the JS thread. This is catastrophic in node.js.

You must use the new Worker Threads API manually to run jimp on a thread.


Finally, a warning regarding selecting image manipulation libraries generally in the node.js world:

From what I've seen, a majority of them end up writing temp files to disk and then invoking a child process to do the actual work, and then read the result back in. (eg something like child_process.exec('imageresizer -in temp/file.jpg -out temp/resized.jpg') ).

This is not an ideal way to do this, and it may be especially surprising when the API looks something like var img = await resizeImg(buffer) , which does not look like it writes to disk.

imagemin is one such library; I would avoid it where performance matters.

Instead, search for modules that implement bindings to native code on the libuv thread pool. This will usually be the most performant way to work with images, since the operations happen on a thread in your node process and with minimal memory copying — and no disk I/O at all.

I've never used it, but node-mozjpeg looks like a good candidate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM