gpt4 book ai didi

javascript - 如何使用jquery在大dom中获得良好的性能?

转载 作者:行者123 更新时间:2023-11-28 19:34:55 25 4
gpt4 key购买 nike

我用 Javascript 和 jquery 编写了一个小程序。

   var mlife = function (i, j, alive) {
var face = '<div class="life" id="l_' + i + '_' + j + '"/>';
var f = $(face);
var ps = new Array();
//var interval;
this.draw=function() {
if (alive) {
f.addClass('l');
} else {
f.removeClass('l');
}
};
this.aliveCheck=function() {
var alivecount = 0;
for (var i = 0; i < ps.length; i++) {
var el = $('#l_' + ps[i]);
if (el.length > 0) {
if (el.hasClass('l')) {
alivecount++;
}
}
}
if (alive) {
if (alivecount < 2) {
alive = false;
} else if (alivecount == 2 || alivecount == 3) {
alive = alive;
} else if (alivecount > 3) {
alive = false;
}
} else {
if (alivecount == 3) {
alive = true;
}
}
//_draw();
};
this.getface = function () {
return f;
};
function _init() {
ps.push((i - 1) + '_' + (j - 1));
ps.push((i - 1) + '_' + (j + 1));
ps.push((i + 1) + '_' + (j - 1));
ps.push((i + 1) + '_' + (j + 1));
ps.push(i + '_' + (j - 1));
ps.push(i + '_' + (j + 1));
ps.push((i + 1) + '_' + j);
ps.push((i - 1) + '_' + j);
if (alive) {
f.addClass('l');
}

};
_init();
}
$(function () {
var html = "";
var alive = false;
var cells = new Array();

for (var i = 0; i < 100; i++) {
for (var j = 0; j < 100; j++) {
if (Math.random() > 0.90) {
alive = true;
} else {
alive = false;
}
var l = new mlife(i, j, alive);

cells.push(l);
$('#show').append(l.getface());
}
}

setInterval(allAliveCheck,1000);


function allAliveCheck(){

for(var i=0;i<cells.length;i++){
cells[i].aliveCheck();
}
for(var i=0;i<cells.length;i++){
cells[i].draw();
}

}







});

http://jsfiddle.net/chanjianyi/upgvzm78/1/

这是关于“生命的游戏”:http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

但我发现控制大量div的性能非常糟糕。

有人有优化我的程序的想法吗?

最佳答案

jQuery 很慢。抛弃 jQuery,它运行得更快。这是一个例子。

var mlife = function(i, j, alive) {
// var face = '<div class="life" id="l_' + i + '_' + j + '"/>';
var f = document.createElement('div');
f.className = 'life';
f.id = 'l_' + i + '_' + j;
var ps = [];
//var interval;
this.draw = function() {
if (alive) {
f.classList.add('l');
} else {
f.classList.remove('l');
}
};
this.aliveCheck = function() {
var alivecount = 0;
for (var i = 0; i < ps.length; i++) {
var el = document.getElementById('l_' + ps[i]);
if (el) {
if (el.classList.contains('l')) {
alivecount++;
}
}
}
if (alive) {
if (alivecount < 2) {
alive = false;
} else if (alivecount == 2 || alivecount == 3) {
alive = alive;
} else if (alivecount > 3) {
alive = false;
}
} else {
if (alivecount == 3) {
alive = true;
}
}
//_draw();
};
this.getface = function() {
return f;
};

function _init() {
ps.push((i - 1) + '_' + (j - 1));
ps.push((i - 1) + '_' + (j + 1));
ps.push((i + 1) + '_' + (j - 1));
ps.push((i + 1) + '_' + (j + 1));
ps.push(i + '_' + (j - 1));
ps.push(i + '_' + (j + 1));
ps.push((i + 1) + '_' + j);
ps.push((i - 1) + '_' + j);
if (alive) {
f.classList.add('l');
}

};
_init();
}


var html = "";
var alive = false;
var cells = [];

for (var i = 0; i < 100; i++) {
for (var j = 0; j < 100; j++) {
if (Math.random() > 0.90) {
alive = true;
} else {
alive = false;
}
var l = new mlife(i, j, alive);

cells.push(l);
document.getElementById('show').appendChild(l.getface());
}
}

setInterval(allAliveCheck, 1000);


function allAliveCheck() {

for (var i = 0; i < cells.length; i++) {
cells[i].aliveCheck();
}
for (var i = 0; i < cells.length; i++) {
cells[i].draw();
}

}
#show {
width: 500px;
height: 500px;
border: 0px solid red;
}
.life {
width: 5px;
height: 5px;
background: black;
float: left;
}
.l {
background: blue;
}
<body>
<div id="show"></div>
</body>

考虑将比赛 field 数据存储为简单数组,而不是将其存储在 HTML 元素中。这样您就可以使任意前端显示相同的数据。 Canvas 或简单的预格式化文本 block 可以在视觉上给出相同的结果;任何一个都可能比一堆 div 表现得更好。

我建议将您的代码发布到 codereview.stackexchange.com 上。这段代码还有很大的改进空间; CR 是学习如何改进它的好地方。

这是一个简单 CA 的示例(这是我几周前为了好玩而做的事情)。它使用一堆 button 元素来显示网格,但请注意如何使用 Canvas 或其他机制轻松交换显示例程。

//
// Cell
//

function Cell(grid, x, y) {
/** @type Grid */
this.grid = grid;
/** @type number */
this.x = x;
/** @type number */
this.y = y;
}

Cell.prototype.getAdjacent = function() {
var cellX = this.x,
cellY = this.y,
grid = this.grid,
result = [],
cell;

for (var y = cellY - 1; y <= cellY + 1; y++) {
for (var x = cellX - 1; x <= cellX + 1; x++) {
if (x == cellX && y == cellY) {
continue;
}
cell = grid.getCell(x, y);
cell && result.push(cell);
}
}
return result;
};

//
// Grid
//

function Grid(width, height) {
/** @type number */
this.width = width;
/** @type number */
this.height = height;
/** @type Array.<Cell> */
this.cells = [];

this.initCells();
}

Grid.prototype.initCells = function() {
var width = this.width,
height = this.height,
cells = this.cells;

for (var y = height; y--;) {
for (var x = width; x--;) {
cells.unshift(new Cell(this, x, y));
}
}
};

Grid.prototype.getCell = function(x, y) {
var width = this.width,
height = this.height,
cells = this.cells;

if (x >= 0 && y >= 0 && x < width && y < height) {
return cells[x + y * width];
}
};

Grid.prototype.each = function(callback) {
var cells = this.cells,
cellCount = cells.length;

for (var i = 0; i < cellCount; i++) {
callback(cells[i], i);
}
};


//
// GridDisplay
//

function GridDisplay(grid, element) {
/** @type Grid */
this.grid = grid;
/** @type Element */
this.element = element;
}

GridDisplay.prototype.drawCell = function(cell) {
var display = this,
button = document.createElement('button');

button.style.border = '1px outset white';
if (cell.value) {
button.style.backgroundColor = 'black';
}
button.onclick = function(event) {
if (cell.value) {
cell.value = false;
} else {
cell.value = true;
}
display.draw();
};

return button;
};

GridDisplay.prototype.draw = function() {
var display = this,
grid = this.grid,
buttons = this.element.getElementsByTagName('button'),
container;

if (buttons.length) {
grid.each(function(cell, i) {
if (cell.value) {
buttons[i].style.backgroundColor = 'black';
} else {
buttons[i].style.backgroundColor = null;
}
});
return;
}

this.element.innerHTML = '';
container = document.createElement('div');

grid.each(function(cell, i) {
if (!cell.x) {
container.appendChild(document.createElement('br'));
}
container.appendChild(display.drawCell(cell));
});

this.element.appendChild(container);
};


//
// Game
//

function Game(width, height, rule) {
// The standard Game of Life is symbolised as "B3/S23":
// A cell is "Born" if it has exactly 3 neighbours,
// "Stays alive" if it has 2 or 3 living neighbours.
var bornSurvive = rule.match(/\d+/g);

this.birthRule = bornSurvive[0];
this.survivalRule = bornSurvive[1];
this.grid = new Grid(width, height);
this.gridDisplay = new GridDisplay(this.grid, board);
this.gridDisplay.draw();
}

Game.prototype.tick = function() {
var grid = this.grid,
survivalRule = this.survivalRule,
birthRule = this.birthRule;

grid.each(function(cell, i) {
var neighbors = cell.getAdjacent();
var liveNeighborCount = 0;

for (var j = 0; j < neighbors.length; j++) {
if (neighbors[j].value) {
++liveNeighborCount;
}
}
if (cell.value) {
cell.nextValue = ~survivalRule.indexOf(liveNeighborCount);
} else {
cell.nextValue = ~birthRule.indexOf(liveNeighborCount);
}
});
grid.each(function(cell) {
cell.value = cell.nextValue;
});
this.gridDisplay.draw();
};


function createButton(label, callback) {
var button = document.createElement('input');
button.type = 'button';
button.value = label;
button.onclick = callback;
document.body.appendChild(button);
}

var board = document.getElementById('board');
var game;
var interval;

createButton('new game', function() {
clearInterval(interval);
board.innerHTML = '';
game = new Game(50, 50, prompt('Enter a rule', 'B3/S23'));
});

createButton('randomize', function() {
var cells = game.grid.cells;
clearInterval(interval);
for (var i = 0; i < cells.length; i++) {
cells[i].value = Math.random() * 2 | 0;
}
game.gridDisplay.draw();
});

createButton('step', function() {
clearInterval(interval);
game.tick();
});

createButton('go', function() {
clearInterval(interval);
interval = setInterval(function () {
game.tick()
}, 100);
});

createButton('stop', function() {
clearInterval(interval);
});
body { font: 10pt sans-serif; }
div { font-size: 1px; }
button { width: 8px; height: 8px; margin:0; padding: 0; vertical-align: middle; }
<div id="board"></div>

关于javascript - 如何使用jquery在大dom中获得良好的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26053469/

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