allow rendering big flame graphs by avoiding stack overflow in JS parser (#684)
* parse as JSON instead of JS, to avoid stack-overflowing the JS parser
* update tests
* add comment
Co-authored-by: Alexey Alexandrov <aalexand@users.noreply.github.com>
diff --git a/internal/driver/webhtml.go b/internal/driver/webhtml.go
index 63df668..9f7a87d 100644
--- a/internal/driver/webhtml.go
+++ b/internal/driver/webhtml.go
@@ -1329,7 +1329,9 @@
<script>viewer(new URL(window.location.href), {{.Nodes}});</script>
<script>{{template "d3flamegraphscript" .}}</script>
<script>
- var data = {{.FlameGraph}};
+ {{- /* Deserialize as JSON instead of a JS object literal because the browser's
+ JSON parser can handle larger payloads than its JS parser. */ -}}
+ var data = JSON.parse("{{.FlameGraph}}");
var width = document.getElementById('chart').clientWidth;
diff --git a/internal/driver/webui_test.go b/internal/driver/webui_test.go
index 81a68ff..58bcd3d 100644
--- a/internal/driver/webui_test.go
+++ b/internal/driver/webui_test.go
@@ -88,8 +88,8 @@
{"/flamegraph", []string{
"File: testbin",
// Check profile frame JSON is included.
- "\"n\":\"root\"",
- "\"n\":\"F1\"",
+ `\\u0022n\\u0022:\\u0022root\\u0022`,
+ `\\u0022n\\u0022:\\u0022F1\\u0022`,
// Check minified d3-flame-graph JS is included.
`flamegraph:\(\)=>`,
// Check d3-flame-graph CSS is included.