From 856e1c600b5a5339af82b1f36d25281fe2dff57c Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 24 Jul 2014 06:32:10 -0700 Subject: [PATCH] Optimize Ref_toString(). I have a large PDF where this function is called 1.6 million times during loading. Minimizing the string concatenations reduces the cumulative allocations done by Firefox within this function from 113 MB to 48 MB. --- src/core/obj.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/obj.js b/src/core/obj.js index 0762adce8..98e7a1722 100644 --- a/src/core/obj.js +++ b/src/core/obj.js @@ -220,7 +220,13 @@ var Ref = (function RefClosure() { Ref.prototype = { toString: function Ref_toString() { - return 'R' + this.num + '.' + this.gen; + // This function is hot, so we make the string as compact as possible. + // |this.gen| is almost always zero, so we treat that case specially. + var str = this.num + 'R'; + if (this.gen !== 0) { + str += this.gen; + } + return str; } };