From 4cfcc38c404d465c18c6588de046a030cf164c96 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Thu, 18 Feb 2010 23:48:50 -0800 Subject: [PATCH] add a destroy() method --- dygraph.js | 30 +++++++++++++++++++++++++++++- tests/perf.html | 7 +++++-- 2 files changed, 34 insertions(+), 3 deletions(-) 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 @@