Javascript 性能:生成ID的平坦父子列表
我正在寻找一种更好的方法来生成亲子关系图;基于特定的id模式 请求Javascript 性能:生成ID的平坦父子列表,javascript,dom,parent-child,circular-dependency,on-the-fly,Javascript,Dom,Parent Child,Circular Dependency,On The Fly,我正在寻找一种更好的方法来生成亲子关系图;基于特定的id模式 请求void 0===cache[parent][child]更快;预期结果: { uuid_1: {uuid_2: {}} uuid_2: {uuid_3: {}, uuid_4: {}} uuid_3: {} uuid_4: {} } HTML结构: <html id="uuid-1"> <body id="uuid-2"&
void 0===cache[parent][child]
更快;预期结果:
{
uuid_1: {uuid_2: {}}
uuid_2: {uuid_3: {}, uuid_4: {}}
uuid_3: {}
uuid_4: {}
}
HTML结构:
<html id="uuid-1">
<body id="uuid-2">
<somewhere>
<whatever id="uuid-3" />
</somewhere>
<foo id="uuid-4" />
</body>
</html>
解析~1300个元素(大菜单结构)以查找我的~50个UUID
使用jQuery尝试1:
_fetch: function(element, factoryName)
{
var a = {}, l = 0, t = this, f = function(el, n)
{
if(!a[n]) a[n] = {};
var e = $(el), test = $('[id^="uuid-"]', e);
if(!test.length)
return;
e.children().each(function()
{
var u = $(this), id = u.attr('id'), q;
// anonymous element: no class defined
if(!(id && 'uuid-' === id.slice(0x00, 0x05)))
{
f(this, n); // continue with current name
return;
}
l++;
q = $.T.util.uuidFromId(id);
$.T.__dict[q] = '#' + id;
a[n][q] = {};
// comment in/out
f(this, q);
});
}
f(element, factoryName);
return a;
}
用黄色JS试试2:
..., g = function(n, p)
{
var r = [];
for(var d = (p || document).getElementsByTagName('*'), i = 0, l = d.length; i < l; i++)
d[i].getAttribute(n) && r.push(d[i]);
return r;
},
f = function(el, n)
{
var z = el.children.length, y = 0;
if(!a[n]) a[n] = {};
if(z && g('id', el)) for(; y < z; y++)
{
var u = el.children[y], id = u.getAttribute('id'), q;
if(!(id && 'uuid-' === id.slice(0x00, 0x05)))
{
f(u, n);
continue;
}
l++;
$.T.__dict[q = $.T.util.uuidFromId(id)] = '#' + id;
a[n][q] = {};
// it's irrelevant to fetch the full html or a sequence by constructor
//f(u, q);
}
}
…,g=函数(n,p)
{
var r=[];
对于(var d=(p | | document).getElementsByTagName('*'),i=0,l=d.length;i
我的问题是:
如何以更快的方式收集DOM元素作为平面表示;像上面的映射吗?我目前的解决方案非常滞后
OT:
基于地图的上下文x对话框:
<baz><alice><bob><bobchild/></bob></alice><foo />
alice._init:
before init children of bob
tell foo 'go away'
before init bob // context: no bob
after init children of alice // && alice without children
after init baz // && baz not ready -> no hello
tell baz 'hello'
爱丽斯._init:
在bob的第一个孩子之前
告诉福“走开”
在init bob//之前上下文:无bob
alice/&&alice无子项的初始子项之后
在初始化baz/&&baz未准备好->否hello之后
告诉baz‘你好’
我仍然不太确定我是否知道您在尝试做什么,但以下是我知道的最快的方法,可以遍历DOM树并积累父/子信息,就像您在构建您希望最终得到的数据结构时所做的那样:
var treeWalkFast = (function() {
// create closure for constants
var skipTags = {"SCRIPT": true, "IFRAME": true, "OBJECT": true,
"EMBED": true, "STYLE": true, "LINK": true, "META": true};
return function(parent, fn, allNodes) {
var parents = [];
var uuidParents = [];
parents.push(parent);
uuidParents.push(parent);
var node = parent.firstChild, nextNode, lastParent;
while (node && node != parent) {
if (allNodes || node.nodeType === 1) {
if (fn(node, parents, uuidParents) === false) {
return(false);
}
}
// if it's an element &&
// has children &&
// has a tagname && is not in the skipTags list
// then, we can enumerate children
if (node.nodeType === 1 && node.firstChild && !(node.tagName && skipTags[node.tagName])) {
// going down one level, add this item to the parent array
parents.push(node);
if (node.id && node.id.substr(0, 5) === "uuid-") {
uuidParents.push(node);
}
node = node.firstChild;
} else if (node.nextSibling) {
// node had no children so going to next sibling
node = node.nextSibling;
} else {
// no child and no nextsibling
// find parent that has a nextSibling
while ((node = node.parentNode) != parent) {
lastParent = parents.pop();
if (lastParent === uuidParents[uuidParents.length - 1]) {
uuidParents.pop();
}
if (node.nextSibling) {
node = node.nextSibling;
break;
}
}
}
}
}
})();
var objects = {uuid_1: {}};
treeWalkFast(document.documentElement, function(node, parents, uuidParents) {
if (node.id && node.id.substr(0, 5) === "uuid-") {
var uuidParent = uuidParents[uuidParents.length - 1];
if (!objects[uuidParent.id]) {
objects[uuidParent.id] = {};
}
objects[uuidParent.id][node.id] = {};
objects[node.id] = {};
}
});
在此进行工作演示:
这是我为之编写的
treeWalkFast()
函数的改编。您的第一块代码是期望的结果吗?你只是想用最快的方式从你的HTML中生成它?还有,你真的想在算法中的
元素中包含一个id吗?而且,为什么数据结构中的该项不将所有其他项显示为子项?是的:第一个代码块是我想要的结果;第二块我的HTML;第三块是\u fetch()
如何查看HTML。子体对象是原型引导加载程序/bootorder。这是所有uuidized元素的构造函数:无映射->元素之间无对话
var treeWalkFast = (function() {
// create closure for constants
var skipTags = {"SCRIPT": true, "IFRAME": true, "OBJECT": true,
"EMBED": true, "STYLE": true, "LINK": true, "META": true};
return function(parent, fn, allNodes) {
var parents = [];
var uuidParents = [];
parents.push(parent);
uuidParents.push(parent);
var node = parent.firstChild, nextNode, lastParent;
while (node && node != parent) {
if (allNodes || node.nodeType === 1) {
if (fn(node, parents, uuidParents) === false) {
return(false);
}
}
// if it's an element &&
// has children &&
// has a tagname && is not in the skipTags list
// then, we can enumerate children
if (node.nodeType === 1 && node.firstChild && !(node.tagName && skipTags[node.tagName])) {
// going down one level, add this item to the parent array
parents.push(node);
if (node.id && node.id.substr(0, 5) === "uuid-") {
uuidParents.push(node);
}
node = node.firstChild;
} else if (node.nextSibling) {
// node had no children so going to next sibling
node = node.nextSibling;
} else {
// no child and no nextsibling
// find parent that has a nextSibling
while ((node = node.parentNode) != parent) {
lastParent = parents.pop();
if (lastParent === uuidParents[uuidParents.length - 1]) {
uuidParents.pop();
}
if (node.nextSibling) {
node = node.nextSibling;
break;
}
}
}
}
}
})();
var objects = {uuid_1: {}};
treeWalkFast(document.documentElement, function(node, parents, uuidParents) {
if (node.id && node.id.substr(0, 5) === "uuid-") {
var uuidParent = uuidParents[uuidParents.length - 1];
if (!objects[uuidParent.id]) {
objects[uuidParent.id] = {};
}
objects[uuidParent.id][node.id] = {};
objects[node.id] = {};
}
});