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
<div id="status"></div>
<script type="text/javascript">
- var num_tests = 100;
+ var num_tests = 250;
var times = [];
var start = new Date;
for (var i = 0; i < num_tests; i++) {
var this_start = new Date;
+ // Calling destroy() here reduces the memory usage in Chrome by
+ // ~1.2MB/instantiation.
+ if (i) g.destroy();
g = new Dygraph(
document.getElementById("div_g"),
NoisyData, {
var end = new Date;
document.getElementById("status").innerHTML = "Elapsed time: " + (end - start)/num_tests + " ms/instantiation";
- new Dygraph(
+ perf = new Dygraph(
document.getElementById("div_g"),
times, {
labels: [ "Iteration", "Time (ms)" ]