我想使用Struts2支持的Json插件展示一个包含1000行的列表。我使用flexigrid (jquery)来解析1000行进行显示,但速度非常慢,有时浏览器会崩溃(Firefox和IE)。
那么,什么是解析大约1000行最快的Javascript框架?
那么,什么是解析大约1000行最快的Javascript框架?
我很好奇去检查了一下,JSON解析时间不是问题;渲染才是问题。以下是一个非常简单(非生产环境)的完全客户端分页示例。在我的上网本上,IE7在36毫秒内解析了1,000行简单的JSON对象,因此即使是复杂对象也不应该成为问题。这是使用Prototype的evalJSON
实现的,它(即使现在)只是将其推迟到eval
并将数据放在括号中(他们将更改这一点)。
1000rows.html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>1,000 Row Test Page</title>
<style type='text/css'>
body {
font-family: sans-serif;
}
#log p {
margin: 0;
padding: 0;
}
</style>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js'></script>
<script type='text/javascript' src='1000rows.js'></script>
</head>
<body><div>
<input type='button' id='btnLoadData' value='Load Data'>
<input type='button' id='btnNext' value='Next'>
<input type='button' id='btnPrevious' value='Previous'>
<table>
<thead>
<tr><th>Name</th><th>Description</th><th>Count</th></tr>
</thead>
<tfoot>
<tr><th colspan='3' id='theLabel'></th></tr>
</tfoot>
<tbody id='theData'>
<tr><td colspan='3'></td></tr>
</tbody>
</table>
<hr>
<div id='log'></div>
</div></body>
</html>
(function() {
var data, windowTop, WINDOW_SIZE;
// "Constant" for the size of our window into the data
WINDOW_SIZE = 20; // Rows
// No data yet
clearData();
// Hook up our observers when we can
document.observe('dom:loaded', function() {
$('btnLoadData').observe('click', loadData);
$('btnNext').observe('click', function(event) {
event.stop();
updateWindow(WINDOW_SIZE);
});
$('btnPrevious').observe('click', function(event) {
event.stop();
updateWindow(-WINDOW_SIZE);
});
});
// Clear our data to a known state
function clearData() {
data = [];
windowTop = 0;
}
// Click handler for the load data button
function loadData() {
var success;
log("Loading data..");
clearData();
updateWindow();
success = false;
// Note: Using text/plain rather than application/json so
// Prototype doesn't parse the data for me, so I can measure
// how long it takes to do it.
new Ajax.Request("data.txt", {
onSuccess: function(response) {
var start, duration;
success = true;
log("Got data, parsing");
start = new Date().getTime();
data = response.responseText.evalJSON();
duration = new Date().getTime() - start;
log("Data parsed in " + duration + "ms");
updateWindow.defer();
}
});
}
function updateWindow(offset) {
var dataElement, labelElement, markup, index, template;
// Get the target element
dataElement = $('theData');
labelElement = $('theLabel');
if (!dataElement || !labelElement) {
return;
}
// If no data, simply say that
if (!data || data.length <= 0) {
dataElement.update("");
labelElement.update("No information");
return;
}
// Ensure that windowTop is rational
if (WINDOW_SIZE > data.length) {
windowTop = 0;
}
else {
if (typeof offset == 'number') {
windowTop += offset;
}
if (windowTop + WINDOW_SIZE > data.length) {
windowTop = data.length - WINDOW_SIZE;
}
if (windowTop < 0) {
windowTop = 0;
}
}
template = new Template(
"<tr><td>#{name}</td><td>#{description}</td><td>#{count}</td></tr>"
);
markup = "";
index = windowTop + WINDOW_SIZE - 1;
if (index >= data.length) {
index = data.length - 1;
}
$('theLabel').update('Showing rows ' + windowTop + ' through ' + index);
while (index >= windowTop) {
markup = template.evaluate(data[index]) + markup;
--index;
}
dataElement.update(markup);
}
// Log a message
function log(msg) {
$('log').appendChild(new Element('p').update(msg));
}
})();
[
{"name": "Name #0001", "description": "Description #0001", "count": 1},
{"name": "Name #0002", "description": "Description #0002", "count": 2},
{"name": "Name #0003", "description": "Description #0003", "count": 3},
...
{"name": "Name #1000", "description": "Description #1000", "count": 1000}
]
1,000行的什么?jQuery实际上相当快,特别是自从版本1.4(仅在几天前发布)进行了性能升级以来。如果您遇到显示1,000行的问题,我首先会问您为什么要显示那么多-没有人应该滚动那么多。其次,所有信息是否都至关重要,并且您只将关键信息传递到JSON值中。最后,您是否通过添加数据的方式使DOM变得不必要地复杂?
同样,如果您只查询需要显示的内容,并且显示合理数量的数据(在屏幕上发布1,000行不合理),那么jQuery将足以满足您的需求。
eval("...");
函数是最快的。不幸的是,它不安全,因为它可以执行恶意javascript代码。
data.split(",")
这样简单。而JSON则非常容易(随着浏览器对显式解析JSON的支持改进,速度和符合性也在提高,因为人们已经意识到仅仅使用eval
是一个坏主意)。此外,我怀疑数据解析并不是真正的问题所在。 - T.J. Crowderdata.split(",")
的东西。 - Gumbo