Closed jimstudt closed 8 years ago
"If your javascript engine allocates a new string and copies the concatenation of this.buf and s into it each time"
That looks like a bug in your JavaScript engine. Maybe file an issue on duktape? Here is V8's string buffer growth, for reference:
FWIW, Hogan used to have code exactly like your suggestion for the benefit of older IE, but it was removed as we dropped support for older, more naive implementations:
https://github.com/twitter/hogan.js/blob/master/web/builds/2.0.0/hogan-2.0.0.js#L172
In the
b(s)
method of theTemplate
prototype, the output is collected using athis.buf += s;
statement for many tiny strings. If your javascript engine allocates a new string and copies the concatenation ofthis.buf
ands
into it each time you will have a quadratic algorithm and a very sad day when you try to make a 4MB output file.In my case, I addressed this by doing this after I had me template…
This is unlikely to be faster than the existing string appending code for short output, but it is a huge difference for large output. In my case I moved from so many minutes that I never saw it complete, to about one second.
Maybe the answer it to document the
.b(s)
and.fl()
methods and include a note about why you might want to do this. Even more performance sensitive people might write directly to a file object in their.b(s)
method, for instance.