Skip to content

Commit

Permalink
Improve performance of arrayBufferToBinaryString
Browse files Browse the repository at this point in the history
* Skip expensive buffer overflows
* Use TextDecoder when possible to speed up conversion
  • Loading branch information
anttipalola committed Mar 22, 2021
1 parent 56e2ff7 commit 2f0b78c
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/modules/addimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import { atob, btoa } from "../libs/AtobBtoa.js";

var UNKNOWN = "UNKNOWN";

// Heuristic limit after which String.fromCharCode will start to overflow.
// Need to change to StringDecoder.
var ARRAY_BUFFER_LIMIT = 6e6;

var imageFileTypeHeaders = {
PNG: [[0x89, 0x50, 0x4e, 0x47]],
TIFF: [
Expand Down Expand Up @@ -730,20 +734,28 @@ import { atob, btoa } from "../libs/AtobBtoa.js";
var arrayBufferToBinaryString = (jsPDFAPI.__addimage__.arrayBufferToBinaryString = function(
buffer
) {
try {
return atob(btoa(String.fromCharCode.apply(null, buffer)));
} catch (e) {
if (
typeof Uint8Array !== "undefined" &&
typeof Uint8Array.prototype.reduce !== "undefined"
) {
return new Uint8Array(buffer)
.reduce(function(data, byte) {
return data.push(String.fromCharCode(byte)), data;
}, [])
.join("");
// Skip direct conversion as it might take many hundred milliseconds before throwing.
if (buffer.length < ARRAY_BUFFER_LIMIT) {
try {
return atob(btoa(String.fromCharCode.apply(null, buffer)));
} catch (e) {
// Buffer was overflown, fall back to other methods
}
}
// Text decoder is much faster than string concatenation or reduce/join.
if (typeof TextDecoder !== "undefined") {
var decoder = new TextDecoder("ascii");
return decoder.decode(buffer);
} else if (
typeof Uint8Array !== "undefined" &&
typeof Uint8Array.prototype.reduce !== "undefined"
) {
return new Uint8Array(buffer)
.reduce(function(data, byte) {
return data.push(String.fromCharCode(byte)), data;
}, [])
.join("");
}
});

/**
Expand Down

0 comments on commit 2f0b78c

Please sign in to comment.