xml与json在javascript中解析并使用,速度大约相差4倍。(在IE中测试的结果)。

其中主要不在于eval,与xml parse的时间差别,主要在于属性访问上的差别:

xml中使用这样的方式 markers[i].getAttribute("info")

与json中使用这样的方式 var info = markers[i].info;

相比, xml要慢不少。

以下为测试的详情:

 

代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
  
<head>
    
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    
<title>Google Maps JavaScript API Example: Simple Map</title>
    
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ-i1QfUnH94QxWIa6N4U6MouMmBA"
            type
="text/javascript"></script>
    
    
<script type="text/javascript">

//全局变量
var map = null;

function initialize() {
    
if (!GBrowserIsCompatible()) return;

    map 
= new GMap2(document.getElementById("map_canvas"));
    map.addMapType( G_PHYSICAL_MAP );
    map.addMapType(G_SATELLITE_3D_MAP);

    map.setCenter(
new GLatLng(39.9116.00), 6);

    map.addControl( 
new GMapTypeControl() );
    map.addControl( 
new GLargeMapControl() );

    map.enableContinuousZoom();

    GEvent.addListener(map, 
"mousemove"function(p){
        
if(!p)return
        window.status 
= "经纬度 " + (Math.round(p.lng()*100000)/ 100000)+ "" 
            
+ (Math.round(p.lat()*100000)/ 100000);
    });

    
var t0 = new Date();
    
for(var i=0; i<100; i++ ){
        
var home_point = new GLatLng(37.056136+i/1e4,114.457626+i/1e4);  
        map.addOverlay(
new GMarker(home_point)); 
    }
    
var t1 = new Date();
    alert(t1
-t0); //1000用时6.3秒

    testSpeed();
}

function testSpeed()
{

//-----------------------------------
    var ary =[];
    
for(var i=0; i<10000; i++ ){
        ary.push(
'<marker ms>>

 

 

相关文章: