Skip to content

Is it expected that mozjpeg: true uses significantly more cpu? #4603

Done

Description

@styfle

Question about an existing feature

Is it expected that setting mozjpeg: true and processing jpeg as both input and output uses significantly more cpu than the default?

What are you trying to achieve?

Trying to find a good default for Next.js users. I noticed really high CPU for JPEG input and output.

In a previous PR, @lovell set mozjpeg to always be true here:

When you searched for similar issues, what did you find that might be related?

https://github.com/lovell/sharp/issues?q=is%3Aissue%20state%3Aclosed%20mozjpeg

Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this question

TODO

Please provide sample image(s) that help explain this question

2,149 JPEGs tested with MozJPEG on and off at quality 75 with concurrency of 8 on MacBook M3 Pro.

CPU averages use each image’s median of three runs on an M3 Pro, including decode/rotate/resize/encode. Ratios average each image’s input KB ÷ output KB; higher means greater size reduction. Smaller images weren’t enlarged.

Requested width Average CPU (ms) CPU saved Average output KB Average input/output ratio
3840 31.67 → 8.80 72.2% 39.57 → 51.81 4.13 → 3.81
1920 20.96 → 7.72 63.2% 25.90 → 33.52 4.51 → 4.09
1080 14.48 → 5.76 60.2% 17.71 → 22.71 5.62 → 4.93
640 11.01 → 5.02 54.4% 12.76 → 16.31 8.31 → 7.09

Disabling MozJPEG saved 54–72% CPU, with average outputs 28–31% larger.

Activity

  1. lovell commented on Sep 11, 2026

    @lovell
    Owner

    Yes, I enabled mozjpeg in vercel/next.js#65846 to ensure feature-parity with squoosh, which always used mozjpeg.

    If CPU cost is now of more concern than bandwidth then either switch off mozjpeg or perhaps look at selectively disabling optimiseScans (whilst keeping mozjpeg enabled).

  2. styfle commented on Sep 11, 2026

    @styfle
    ContributorAuthor

    Thanks! I tried with both mozjpeg: true and optimizeScans: false and there was some improvement so that might be a good compromise since there's barely a size increase.

    Width Average CPU (ms) CPU saved Average output KB Size increase
    3840 32.03 → 23.76 25.8% 39.57 → 41.29 4.3%
    1920 21.24 → 16.46 22.5% 25.90 → 26.82 3.6%
    1080 14.66 → 11.60 20.8% 17.71 → 18.35 3.6%
    640 11.18 → 9.13 18.4% 12.76 → 13.22 3.6%

    It looks like mozjpeg sets about 5 different settings so I might have to try to benchmark all the combinations.

    The thing to note about squoosh was that we always used sharp for production builds (next build) and squoosh was only used for development builds (next dev). Now of course, sharp is used for both.

    I'm tempted to just turn of mozjpeg completely since it seems like no one noticed the difference between on vs off.

    How did you decide the default settings for sharp?

  3. lovell commented on Sep 13, 2026

    @lovell
    Owner

    How did you decide the default settings for sharp?

    The default is all mozjeg features off, which is the same as using libjpeg-turbo (upon which mozjpeg is based), as that's what sharp provided for many years.

    Switching mozjpeg on is syntactic sugar to set all the mozjpeg-related options to their mozjpeg defaults:

    sharp/lib/output.mjs

    Lines 637 to 643 in 9dcb3dd

    if (options.mozjpeg) {
    this.options.jpegTrellisQuantisation = true;
    this.options.jpegOvershootDeringing = true;
    this.options.jpegOptimiseScans = true;
    this.options.jpegProgressive = true;
    this.options.jpegQuantisationTable = 3;
    }

  4. added this to the v0.35.6 milestone on Nov 13, 2026
  5. lovell commented on Nov 28, 2026

    @lovell
    Owner

    v0.35.6 now available, thanks for the PR to improve the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions