From: Dan Vanderkam Date: Fri, 19 Feb 2010 07:48:50 +0000 (-0800) Subject: add a destroy() method X-Git-Tag: v1.0.0~727 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=4cfcc38c404d465c18c6588de046a030cf164c96;p=dygraphs.git add a destroy() method --- diff --git a/dygraph.js b/dygraph.js index 5e824ae..504cee7 100644 --- a/dygraph.js +++ b/dygraph.js @@ -351,7 +351,35 @@ Dygraph.prototype.createInterface_ = function() { this.createStatusMessage_(); this.createRollInterface_(); this.createDragInterface_(); -} +}; + +/** + * Detach DOM elements in the dygraph and null out all data references. + * Calling this when you're done with a dygraph can dramatically reduce memory + * usage. See, e.g., the tests/perf.html example. + */ +Dygraph.prototype.destroy = function() { + var removeRecursive = function(node) { + while (node.hasChildNodes()) { + removeRecursive(node.firstChild); + node.removeChild(node.firstChild); + } + }; + removeRecursive(this.maindiv_); + + var nullOut = function(obj) { + for (var n in obj) { + if (typeof(obj[n]) === 'object') { + obj[n] = null; + } + } + }; + + // These may not all be necessary, but it can't hurt... + nullOut(this.layout_); + nullOut(this.plotter_); + nullOut(this); +}; /** * Creates the canvas containing the PlotKit graph. Only plotkit ever draws on diff --git a/tests/perf.html b/tests/perf.html index 6515dee..e0f487c 100644 --- a/tests/perf.html +++ b/tests/perf.html @@ -14,12 +14,15 @@