From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../tools/memory/dom_allocation_example/index.html | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 files/zh-cn/tools/memory/dom_allocation_example/index.html (limited to 'files/zh-cn/tools/memory/dom_allocation_example') diff --git a/files/zh-cn/tools/memory/dom_allocation_example/index.html b/files/zh-cn/tools/memory/dom_allocation_example/index.html new file mode 100644 index 0000000000..29c92782ef --- /dev/null +++ b/files/zh-cn/tools/memory/dom_allocation_example/index.html @@ -0,0 +1,60 @@ +--- +title: DOM allocation example +slug: Tools/Memory/DOM_allocation_example +tags: + - 内存 + - 开发者工具 +translation_of: Tools/Memory/DOM_allocation_example +--- +
{{ToolsSidebar}}
+ +

这篇文章描述了一个简单的 Web 页面,它被用于阐述内存工具的一些特性。

+ +

你可以在这个网页中试试:https://mdn.github.io/performance-scenarios/dom-allocs/alloc.html

+ +

它仅仅包含了一个创建大量 DOM 节点的脚本:

+ +
var toolbarButtonCount = 20;
+var toolbarCount = 200;
+
+function getRandomInt(min, max) {
+    return Math.floor(Math.random() * (max - min + 1)) + min;
+}
+
+function createToolbarButton() {
+  var toolbarButton = document.createElement("span");
+  toolbarButton.classList.add("toolbarbutton");
+  // stop Spidermonkey from sharing instances
+  toolbarButton[getRandomInt(0,5000)] = "foo";
+  return toolbarButton;
+}
+
+function createToolbar() {
+  var toolbar = document.createElement("div");
+  // stop Spidermonkey from sharing instances
+  toolbar[getRandomInt(0,5000)] = "foo";
+  for (var i = 0; i < toolbarButtonCount; i++) {
+    var toolbarButton = createToolbarButton();
+    toolbar.appendChild(toolbarButton);
+  }
+  return toolbar;
+}
+
+function createToolbars() {
+  var container = document.getElementById("container");
+  for (var i = 0; i < toolbarCount; i++) {
+    var toolbar = createToolbar();
+    container.appendChild(toolbar);
+  }
+}
+
+createToolbars();
+
+ +

一段表示代码工作流程的伪代码:

+ +
createToolbars()
+    -> createToolbar() // 调用 200 次, 每次创建一个 DIV 元素
+       -> createToolbarButton() // 每次调用 createToolbar() 时,调用 20 次,每次创建一个 SPAN 元素
+ +

它总共创建了 200 个 HTMLDivElement 对象和 4000 个 HTMLSpanElement 对象。

-- cgit v1.2.3-54-g00ecf