Skip to content

Commit

Permalink
Optimize CMap.prototype.forEach().
Browse files Browse the repository at this point in the history
This change avoids the element stringification caused by for..in for the
vast majority of CMaps.

When loading the PDF from issue mozilla#4580, this change reduces peak RSS from ~650
to ~600 MiB, and improves overall speed by ~20%, from 902 ms to 713 ms.  Other
CMap-heavy documents will also see improvements.
  • Loading branch information
nnethercote committed Jul 30, 2014
1 parent b86daed commit 28687bc
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/core/cmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,24 @@ var CMap = (function CMapClosure() {
},

forEach: function(callback) {
// Most maps have fewer than 65536 entries, and for those we use normal
// array iteration. But really sparse tables are possible -- e.g. with
// indices in the *billions*. For such tables we use for..in, which isn't
// ideal because it stringifies the indices for all present elements, but
// it does avoid iterating over every undefined entry.
var map = this._map;
for (var key in this._map) {
callback(key, map[key]);
var length = map.length;
var i;
if (length <= 0x10000) {
for (i = 0; i < length; i++) {
if (map[i] !== undefined) {
callback(i, map[i]);
}
}
} else {
for (i in this._map) {
callback(i, map[i]);
}
}
},

Expand Down

0 comments on commit 28687bc

Please sign in to comment.