mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-25 17:48:07 +02:00
Reduce unnecessary usage of Array.prototype.concat()
There are obviously cases where using `concat` makes perfect sense, since that method doesn't change any of the existing Arrays; see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat However, in a few cases throughout the code-base that's not an issue and using `concat` only leads to unnecessary intermediate allocations. With modern JavaScript we can thus replace those with a combination of `push` and spread-syntax, which wasn't originally possible when the code was written.
This commit is contained in:
parent
f516bb2174
commit
c21f4faaf8
4 changed files with 21 additions and 23 deletions
|
@ -1387,7 +1387,7 @@ class CFFCompiler {
|
|||
const output = {
|
||||
data: [],
|
||||
length: 0,
|
||||
add: function CFFCompiler_add(data) {
|
||||
add(data) {
|
||||
this.data = this.data.concat(data);
|
||||
this.length = this.data.length;
|
||||
},
|
||||
|
@ -1680,7 +1680,7 @@ class CFFCompiler {
|
|||
}
|
||||
|
||||
compileDict(dict, offsetTracker) {
|
||||
let out = [];
|
||||
const out = [];
|
||||
// The dictionary keys must be in a certain order.
|
||||
const order = dict.order;
|
||||
for (let i = 0; i < order.length; ++i) {
|
||||
|
@ -1708,7 +1708,7 @@ class CFFCompiler {
|
|||
switch (type) {
|
||||
case "num":
|
||||
case "sid":
|
||||
out = out.concat(this.encodeNumber(value));
|
||||
out.push(...this.encodeNumber(value));
|
||||
break;
|
||||
case "offset":
|
||||
// For offsets we just insert a 32bit integer so we don't have to
|
||||
|
@ -1720,20 +1720,20 @@ class CFFCompiler {
|
|||
if (!offsetTracker.isTracking(name)) {
|
||||
offsetTracker.track(name, out.length);
|
||||
}
|
||||
out = out.concat([0x1d, 0, 0, 0, 0]);
|
||||
out.push(0x1d, 0, 0, 0, 0);
|
||||
break;
|
||||
case "array":
|
||||
case "delta":
|
||||
out = out.concat(this.encodeNumber(value));
|
||||
out.push(...this.encodeNumber(value));
|
||||
for (let k = 1, kk = values.length; k < kk; ++k) {
|
||||
out = out.concat(this.encodeNumber(values[k]));
|
||||
out.push(...this.encodeNumber(values[k]));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new FormatError(`Unknown data type of ${type}`);
|
||||
}
|
||||
}
|
||||
out = out.concat(dict.opcodes[key]);
|
||||
out.push(...dict.opcodes[key]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue