From f247e8f1b8faf4ba23a4a5523eaa83b8e094bf93 Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Wed, 17 Jun 2009 03:20:42 +0000 Subject: [PATCH] Further optimize the base64 encoding function (precalculate array size needed and avoid push) git-svn-id: http://svg-edit.googlecode.com/svn/trunk@138 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/svgcanvas.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/editor/svgcanvas.js b/editor/svgcanvas.js index 0ab9ef3d..d4cb0cb5 100644 --- a/editor/svgcanvas.js +++ b/editor/svgcanvas.js @@ -84,6 +84,7 @@ function SvgCanvas(c) } var svgToString = function(elem, indent) { + // TODO: could use the array.join() optimization trick here too var out = ""; if (elem) { var attrs = elem.attributes; @@ -792,15 +793,17 @@ var Utils = { // public domain. It would be nice if you left this header intact. // Base64 code from Tyler Akins -- http://rumkin.com -// schiller: Removed string concatenation in favour of Array.join() optimization +// schiller: Removed string concatenation in favour of Array.join() optimization, +// also precalculate the size of the array needed. "_keyStr" : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", "encode64" : function(input) { - var output = []; + // base64 strings are 4/3 larger than the original string + var output = new Array(parseInt(input.length*4/3)); var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; - var i = 0; + var i = 0, p = 0; do { chr1 = input.charCodeAt(i++); @@ -818,10 +821,10 @@ var Utils = { enc4 = 64; } - output.push(this._keyStr.charAt(enc1)); - output.push(this._keyStr.charAt(enc2)); - output.push(this._keyStr.charAt(enc3)); - output.push(this._keyStr.charAt(enc4)); + output[p++] = this._keyStr.charAt(enc1); + output[p++] = this._keyStr.charAt(enc2); + output[p++] = this._keyStr.charAt(enc3); + output[p++] = this._keyStr.charAt(enc4); } while (i < input.length); return output.join('');