Browse Source

Improve memory-efficiency of DOMElement_toString in domstubs

Test case:
Using the PDF file from https://github.com/mozilla/pdf.js/issues/8534
node --max_old_space_size=200 examples/node/pdf2svg.js /tmp/FatalProcessOutOfMemory.pdf

Before this patch:
Node.js crashes due to OOM after processing 10 pages.

After this patch:
Node.js crashes due to OOM after processing 19 pages.
Rob Wu 8 years ago
parent
commit
849d8cfa24
  1. 29
      examples/node/domstubs.js

29
examples/node/domstubs.js

@ -91,24 +91,27 @@ DOMElement.prototype = {
}, },
toString: function DOMElement_toString() { toString: function DOMElement_toString() {
var attrList = []; var buf = [];
for (i in this.attributes) { buf.push('<' + this.nodeName);
attrList.push(i + '="' + xmlEncode(this.attributes[i]) + '"'); if (this.nodeName === 'svg:svg') {
buf.push(' xmlns:xlink="http://www.w3.org/1999/xlink"' +
' xmlns:svg="http://www.w3.org/2000/svg"');
} }
for (var i in this.attributes) {
buf.push(' ' + i + '="' + xmlEncode(this.attributes[i]) + '"');
}
buf.push('>');
if (this.nodeName === 'svg:tspan' || this.nodeName === 'svg:style') { if (this.nodeName === 'svg:tspan' || this.nodeName === 'svg:style') {
var encText = xmlEncode(this.textContent); buf.push(xmlEncode(this.textContent));
return '<' + this.nodeName + ' ' + attrList.join(' ') + '>' +
encText + '</' + this.nodeName + '>';
} else if (this.nodeName === 'svg:svg') {
var ns = 'xmlns:xlink="http://www.w3.org/1999/xlink" ' +
'xmlns:svg="http://www.w3.org/2000/svg"'
return '<' + this.nodeName + ' ' + ns + ' ' + attrList.join(' ') + '>' +
this.childNodes.join('') + '</' + this.nodeName + '>';
} else { } else {
return '<' + this.nodeName + ' ' + attrList.join(' ') + '>' + this.childNodes.forEach(function(childNode) {
this.childNodes.join('') + '</' + this.nodeName + '>'; buf.push(childNode.toString());
});
} }
buf.push('</' + this.nodeName + '>');
return buf.join('');
}, },
cloneNode: function DOMElement_cloneNode() { cloneNode: function DOMElement_cloneNode() {

Loading…
Cancel
Save