Javascript 哪一个最快?使用XML的xhr还是动态添加脚本文件?
我有一个文件“script.php”,它打印数据库中的数据,但我想用javascript动态加载和处理文件中的数据。 创建新的脚本标记(script.php的内容类型为text/javascript)最快吗 使用javascript数组中的数据或通过XHR接收XML格式的数据,如(“script.php”的内容类型为text/XML) ?Javascript 哪一个最快?使用XML的xhr还是动态添加脚本文件?,javascript,xmlhttprequest,Javascript,Xmlhttprequest,我有一个文件“script.php”,它打印数据库中的数据,但我想用javascript动态加载和处理文件中的数据。 创建新的脚本标记(script.php的内容类型为text/javascript)最快吗 使用javascript数组中的数据或通过XHR接收XML格式的数据,如(“script.php”的内容类型为text/XML) ? 谢谢您可能希望通过Ajax和更好的JSON格式发送数据库结果,这样服务器端的JavaScript函数就可以轻松处理这些结果 此选项允许您在代码中设置备用部分,
谢谢您可能希望通过Ajax和更好的JSON格式发送数据库结果,这样服务器端的JavaScript函数就可以轻松处理这些结果
此选项允许您在代码中设置备用部分,以防发生错误。XML在JavaScript中的使用速度总是很慢,因为您需要使用DOM解析器来读取它,因此在使用JavaScript时总是首选JSON 离题: 在向动态脚本标记提供数据时,您应该注意安全风险,因为您的站点可能会受到某些XSRF攻击,因为在使用XHR时会忽略相同的源策略 通过添加一段时间(1)来避免这种情况;在您的响应顶部,如果不可能使用来自某个邪恶站点的脚本标记来使用您的数据。当然,在使用数据之前,必须先删除此项 祝你好运:)--UPDATED--优化代码--- 我做了一个小的基准测试,下面是结果 对于1000次迭代 XML对象花费了4270毫秒 添加一个简单的脚本标记需要4169ms 使用Eval函数的强大XHR给出了3206ms;//最快的 这里有客户端和服务器端脚本供参考 脚本2.php
<?php
echo trim('
var dunce = {
menu : {
id:1,
gohan:"goku blah blah blah"
}
};
console.log(dunce.menu.id); // yeah its still faster
iteration++;
if(iteration<1000){
ScriptTAG();
}else{
console.log("Total time taken for "+iteration+"iterations is "+ (new
Date().getTime()-start) );
}');
?>
客户端文件
var iteration = 0;
// Use XHR
var start ;
// Use console for firing these
function XHR() {
if(iteration == 0) {
start = new Date().getTime();
}
var io = new XMLHttpRequest();
io.open("POST",'script.php',true);
io.onload = function() {
iteration++;
if(iteration<1000) {
XHR();
} else {
alert("Total time taken for "+iteration+"iterations is "+ (new Date().getTime()-start) );
}
}
io.send();
}
function AlterXHR(){
// EVAL IDEA
if(iteration == 0){
start = new Date().getTime();
}
var io = new XMLHttpRequest();
io.open("POST",'script2.php',true);
io.onload = function(){
eval(io.responseText);
}
io.send();
}
function ScriptTAG() {
if(iteration == 0 ) {
start = new Date().getTime();
}
var script = document.createElement('script');
script.src = "script2.php";
document.head.appendChild(script);
}
ScriptTAG(); // Replace this with whatever u want to test! and run!!
var迭代=0;
//使用XHR
var启动;
//使用控制台触发这些命令
函数XHR(){
如果(迭代==0){
开始=新日期().getTime();
}
var io=新的XMLHttpRequest();
open(“POST”、'script.php',true);
io.onload=函数(){
迭代++;
如果(迭代为什么script.php有content-type text/javascript?按照XHR的方式,你有更好的基于事件的系统,有条件告诉你它是否失败,但是没有方法用script-tag的方式检查上面的内容。它还允许你以比script-tag更好的方式处理输出!@Raynos这是个好问题我想知道的是,他可能只是发送了一个JSON对象,比如say content={“x”:“blah”};@Raynos:因为如果它输出Javascript,它就需要这样做。这有什么问题吗?@JOn,我支持Raynos的观点,我的意思是,使用脚本的求值而不是使用简单的运行时,通过脚本标记加载文档片段有什么意义?
<?php
echo trim('
var dunce = {
menu : {
id:1,
gohan:"goku blah blah blah"
}
};
console.log(dunce.menu.id); // yeah its still faster
iteration++;
if(iteration<1000){
ScriptTAG();
}else{
console.log("Total time taken for "+iteration+"iterations is "+ (new
Date().getTime()-start) );
}');
?>
<?php
header ("Content-Type:text/xml");
echo"<menu><id>1</id><gohan>Goku blah blah blah</gohan></menu>";
?>
var iteration = 0;
// Use XHR
var start ;
// Use console for firing these
function XHR() {
if(iteration == 0) {
start = new Date().getTime();
}
var io = new XMLHttpRequest();
io.open("POST",'script.php',true);
io.onload = function() {
iteration++;
if(iteration<1000) {
XHR();
} else {
alert("Total time taken for "+iteration+"iterations is "+ (new Date().getTime()-start) );
}
}
io.send();
}
function AlterXHR(){
// EVAL IDEA
if(iteration == 0){
start = new Date().getTime();
}
var io = new XMLHttpRequest();
io.open("POST",'script2.php',true);
io.onload = function(){
eval(io.responseText);
}
io.send();
}
function ScriptTAG() {
if(iteration == 0 ) {
start = new Date().getTime();
}
var script = document.createElement('script');
script.src = "script2.php";
document.head.appendChild(script);
}
ScriptTAG(); // Replace this with whatever u want to test! and run!!