From f5a617a3341285b84655fe6cab8fa35acf12748e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Tue, 28 Jan 2020 14:23:58 +0100 Subject: [PATCH] Make the `decodeHuffman` function, in `src/core/jpg.js`, slightly more efficient Rather than repeating the `typeof node` check twice, we can use a `switch` statement instead. This patch was tested using the PDF file from issue 3809, i.e. https://web.archive.org/web/20140801150504/http://vs.twonky.dk/invitation.pdf, with the following manifest file: ``` [ { "id": "issue3809", "file": "../web/pdfs/issue3809.pdf", "md5": "", "rounds": 50, "type": "eq" } ] ``` which gave the following results when comparing this patch against the `master` branch: ``` -- Grouped By browser, stat -- browser | stat | Count | Baseline(ms) | Current(ms) | +/- | % | Result(P<.05) ------- | ------------ | ----- | ------------ | ----------- | --- | ----- | ------------- Firefox | Overall | 50 | 12537 | 12451 | -86 | -0.69 | faster Firefox | Page Request | 50 | 5 | 5 | 0 | 0.77 | Firefox | Rendering | 50 | 12532 | 12446 | -86 | -0.69 | faster ``` --- src/core/jpg.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/core/jpg.js b/src/core/jpg.js index 2aa37a9c3..5f973ba6e 100644 --- a/src/core/jpg.js +++ b/src/core/jpg.js @@ -178,12 +178,13 @@ var JpegImage = (function JpegImageClosure() { var node = tree; while (true) { node = node[readBit()]; - if (typeof node === "number") { - return node; - } - if (typeof node !== "object") { - throw new JpegError("invalid huffman sequence"); + switch (typeof node) { + case "number": + return node; + case "object": + continue; } + throw new JpegError("invalid huffman sequence"); } }