gpt4 book ai didi

javascript - jQuery .each 在 Safari 上比 Chrome/Firefox 慢

转载 作者:太空宇宙 更新时间:2023-11-04 16:18:27 24 4
gpt4 key购买 nike

我有一个大型 HTML 表格(1,000-1,500 行,40 列宽)。我有一些输入和选择框,以便用户可以过滤行。附加的相关 javascript/jquery (注意:并未粘贴整个代码库,因为它不是瓶颈)如下所示:

function autoRank() {
// auto number
rank = 0;
$("#myTablePlayers .playerData").each(function() {
if ($(this).css("display") != "none") {
rank++;
$(this).find('td').eq(colRank).text(rank);
}
});
}

var teamCols = $(),
GPCols = $(),
posCols = $(),
ageCols = $();

$("#myTablePlayers .playerData").each(function() {
var columns = $(this).find('td');
teamCols = teamCols.add($(".colTeam", this));
GPCols = GPCols.add(columns.eq(colGP));
posCols = posCols.add(columns.eq(colPos));
ageCols = ageCols.add(columns.eq(colAge))
});

function filterTable() {
// Need some error checking on input not number
minGP = $("#mingp").val()
teams = $("#teamFilter").val().toUpperCase()
position = $("#position").val()
age = $("#age").val()

$("#myTablePlayers .playerData").show();

/* Loop through to check for teams */
if (teams) {
teamCols.each(function() {
if (!this.innerHTML.toUpperCase().includes(teams)) {
$(this).parent().hide();
}
});
}

/* Loop and check for min GP */
GPCols.each(function() {
if ( Number(this.innerHTML) < minGP) {
$(this).parent().hide();
}
});

/* Check for age requirement */
if (age) {
age = Number(age)
ageCols.each(function() {
thisAge = Number(this.innerHTML);
if ( thisAge < age || thisAge >= age+1 ) {
$(this).parent().hide();
}
});
}

/* Check the position requirement */
if (position) {
posCols.each(function() {
var thisPos = this.innerHTML
if (position == "D") {
if (thisPos.indexOf("D") == -1) {
$(this).parent().hide();
}
} else if (position == "F") {
if (thisPos.indexOf("D") != -1) {
$(this).parent().hide();
}
} else if (thisPos != position) {
$(this).parent().hide();
}
});
}

autoRank();
}

当尽可能少地剥离代码时,有问题的代码是

var.each(function() { ...

filterTable()函数中。

当我在 Chrome 或 Firefox 上运行它时,它运行得很快(不到 1 秒)并且 DOM 被正确渲染。当我在 Safari 上执行时,需要 30 多秒。

这是为什么?我可以做些什么来适应这个浏览器?

<小时/>

jQuery:1.11.1(即使升级到 3.1.1 后也会出现同样的问题)。

Safari:10.0.1
火狐浏览器:50
Chrome:54.0。

最佳答案

从代码中删除所有重复和不必要的复杂性后,剩下的就是:

var colRank = 0, colTeam = 1, colGP = 2, colAge = 3, colPos = 4;

function filterTable() {
var minGP = +$("#mingp").val();
var age = +$("#age").val();
var teams = $("#teamFilter").val().toUpperCase();
var position = $("#position").val();
var rank = 0;

$("#myTablePlayers .playerData").each(function () {
if (
(teams && this.cells[colTeam].textContent.toUpperCase().includes(teams)) ||
(minGP && +this.cells[colGP].textContent < minGP) ||
(age && (+this.cells[colAge].textContent < age || +this.cells[colAge].textContent >= age+1)) ||
((position === "D" || position === "F") && this.cells[colPos].textContent.indexOf(position) === -1) ||
(!(position === "D" || position === "F") && (this.cells[colPos].textContent !== position))
) {
this.cells[colRank].textContent = ++rank;
this.style.display = "";
} else {
this.style.display = "none";
}
});
}

我已经删除了几乎所有 jQuery,转而使用 native DOM 操作。

剩余的 .each() 可以调整为 document.getElementById('myTablePlayers').tBodies[0] 上的普通旧 for 循环.rows,如果您想发挥最后一点可能的性能。

按可能性对 if 条件重新排序:从通常过滤掉最多行的条件到过滤掉最少行的条件。因为JS对条件进行了短路,所以整体检查的条件较少。

制作 table display: fixed还可以以牺牲灵 active 为代价来提高渲染性能。

最后,您可以使用CSS to do counters 。这可能比手动设置表格单元格的内容更快。自己测试一下。

关于javascript - jQuery .each 在 Safari 上比 Chrome/Firefox 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40870513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com