Skip to content

Instantly share code, notes, and snippets.

@mishoo
Created August 19, 2012 10:18
Show Gist options
  • Save mishoo/3394129 to your computer and use it in GitHub Desktop.
Save mishoo/3394129 to your computer and use it in GitHub Desktop.
// Explaining https://twitter.com/mcbazon/status/236504096868286464
// I have a simple output stream implementation that keeps track
// of current line/col numbers. It accumulates text in the OUTPUT
// variable, and at times I needed to know the last character
// that was added. I had this function:
function last_char() {
return OUTPUT.charAt(OUTPUT.length - 1);
};
// Noticed that it was really slow in Chrome when the output got to
// about 200K. Changing it to:
function last_char() {
return last.charAt(last.length - 1);
}
// where in `last' I'm storing just the previously added chunk
// made it about 10 times faster (0.2s instead of 2.6s).
// tested with V8 (NodeJS v0.7.10-pre)
// according to my benchmark at http://jsperf.com/string-charat-length
// FF doesn't seem to exhibit the "long string problem" and it's also much
// faster overall.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment