Skip to content

Commit fda7e3b

Browse files
authored
Replaced concat with push for better performance per #682 (#683)
1 parent 3fce140 commit fda7e3b

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

src/tables/cmap.js

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,14 @@ function makeCmapTable(glyphs) {
277277
];
278278

279279
if (!isPlan0Only)
280-
cmapTable = cmapTable.concat([
280+
cmapTable.push(...[
281281
// CMAP 12 header
282282
{name: 'cmap12PlatformID', type: 'USHORT', value: 3}, // We encode only for PlatformID = 3 (Windows) because it is supported everywhere
283283
{name: 'cmap12EncodingID', type: 'USHORT', value: 10},
284284
{name: 'cmap12Offset', type: 'ULONG', value: 0}
285285
]);
286286

287-
cmapTable = cmapTable.concat([
287+
cmapTable.push(...[
288288
// CMAP 4 Subtable
289289
{name: 'format', type: 'USHORT', value: 4},
290290
{name: 'cmap4Length', type: 'USHORT', value: 0},
@@ -303,11 +303,10 @@ function makeCmapTable(glyphs) {
303303
for (let j = 0; j < glyph.unicodes.length; j += 1) {
304304
addSegment(t, glyph.unicodes[j], i);
305305
}
306-
307-
t.segments = t.segments.sort(function (a, b) {
308-
return a.start - b.start;
309-
});
310306
}
307+
t.segments.sort(function (a, b) {
308+
return a.start - b.start;
309+
});
311310

312311
addTerminatorSegment(t);
313312

@@ -334,12 +333,12 @@ function makeCmapTable(glyphs) {
334333

335334
// CMAP 4
336335
if (segment.end <= 65535 && segment.start <= 65535) {
337-
endCounts = endCounts.concat({name: 'end_' + i, type: 'USHORT', value: segment.end});
338-
startCounts = startCounts.concat({name: 'start_' + i, type: 'USHORT', value: segment.start});
339-
idDeltas = idDeltas.concat({name: 'idDelta_' + i, type: 'SHORT', value: segment.delta});
340-
idRangeOffsets = idRangeOffsets.concat({name: 'idRangeOffset_' + i, type: 'USHORT', value: segment.offset});
336+
endCounts.push({name: 'end_' + i, type: 'USHORT', value: segment.end});
337+
startCounts.push({name: 'start_' + i, type: 'USHORT', value: segment.start});
338+
idDeltas.push({name: 'idDelta_' + i, type: 'SHORT', value: segment.delta});
339+
idRangeOffsets.push({name: 'idRangeOffset_' + i, type: 'USHORT', value: segment.offset});
341340
if (segment.glyphId !== undefined) {
342-
glyphIds = glyphIds.concat({name: 'glyph_' + i, type: 'USHORT', value: segment.glyphId});
341+
glyphIds.push({name: 'glyph_' + i, type: 'USHORT', value: segment.glyphId});
343342
}
344343
} else {
345344
// Skip Unicode > 65535 (16bit unsigned max) for CMAP 4, will be added in CMAP 12
@@ -349,9 +348,9 @@ function makeCmapTable(glyphs) {
349348
// CMAP 12
350349
// Skip Terminator Segment
351350
if (!isPlan0Only && segment.glyphIndex !== undefined) {
352-
cmap12Groups = cmap12Groups.concat({name: 'cmap12Start_' + i, type: 'ULONG', value: segment.start});
353-
cmap12Groups = cmap12Groups.concat({name: 'cmap12End_' + i, type: 'ULONG', value: segment.end});
354-
cmap12Groups = cmap12Groups.concat({name: 'cmap12Glyph_' + i, type: 'ULONG', value: segment.glyphIndex});
351+
cmap12Groups.push({name: 'cmap12Start_' + i, type: 'ULONG', value: segment.start});
352+
cmap12Groups.push({name: 'cmap12End_' + i, type: 'ULONG', value: segment.end});
353+
cmap12Groups.push({name: 'cmap12Glyph_' + i, type: 'ULONG', value: segment.glyphIndex});
355354
}
356355
}
357356

@@ -361,12 +360,22 @@ function makeCmapTable(glyphs) {
361360
t.entrySelector = Math.log(t.searchRange / 2) / Math.log(2);
362361
t.rangeShift = t.segCountX2 - t.searchRange;
363362

364-
t.fields = t.fields.concat(endCounts);
363+
for (let i = 0; i < endCounts.length; i++) {
364+
t.fields.push(endCounts[i]);
365+
}
365366
t.fields.push({name: 'reservedPad', type: 'USHORT', value: 0});
366-
t.fields = t.fields.concat(startCounts);
367-
t.fields = t.fields.concat(idDeltas);
368-
t.fields = t.fields.concat(idRangeOffsets);
369-
t.fields = t.fields.concat(glyphIds);
367+
for (let i = 0; i < startCounts.length; i++) {
368+
t.fields.push(startCounts[i]);
369+
}
370+
for (let i = 0; i < idDeltas.length; i++) {
371+
t.fields.push(idDeltas[i]);
372+
}
373+
for (let i = 0; i < idRangeOffsets.length; i++) {
374+
t.fields.push(idRangeOffsets[i]);
375+
}
376+
for (let i = 0; i < glyphIds.length; i++) {
377+
t.fields.push(glyphIds[i]);
378+
}
370379

371380
t.cmap4Length = 14 + // Subtable header
372381
endCounts.length * 2 +
@@ -382,15 +391,18 @@ function makeCmapTable(glyphs) {
382391
cmap12Groups.length * 4;
383392

384393
t.cmap12Offset = 12 + (2 * 2) + 4 + t.cmap4Length;
385-
t.fields = t.fields.concat([
394+
t.fields.push(...[
386395
{name: 'cmap12Format', type: 'USHORT', value: 12},
387396
{name: 'cmap12Reserved', type: 'USHORT', value: 0},
388397
{name: 'cmap12Length', type: 'ULONG', value: cmap12Length},
389398
{name: 'cmap12Language', type: 'ULONG', value: 0},
390399
{name: 'cmap12nGroups', type: 'ULONG', value: cmap12Groups.length / 3}
391400
]);
392401

393-
t.fields = t.fields.concat(cmap12Groups);
402+
for (let i = 0; i < cmap12Groups.length; i++) {
403+
t.fields.push(cmap12Groups[i]);
404+
}
405+
394406
}
395407

396408
return t;

0 commit comments

Comments
 (0)