Archive for the ‘Javascript’ Category

JSON vs XML parsing performance

Monday, October 1st, 2007

In one project I worked on is using XML format to pass data and handle them inside HTML web page with a MSXML parser object. I was thought if I pass the data in JSON format directly could be faster. However, after I wrote a simple test code to verify the idea, I found I was wrong.

I believe it’s because XML parsing is inside MSXML object and JSON parsing is using Jscript engine which have more overhead.

<html>
<head>
<title>JSON vs XML parse performance in browser</title>
<script>

function out(str)
{

  var o = document.getElementById("output");

  if (o!=null)
    o.innerHTML += str + "<br/>";

}

function generateJSON(size)
{
  var strJSON = "[";
  ch = ”;
  for (i = 0 ; i < size; i++)
  {
   strJSON += ch + ‘{"filename":"file’ + i + ‘.html"}’;
   ch = ‘,’;
  }
  strJSON += ‘]’;

  return strJSON;
}

function generateXML(size)
{
  var strXML = "<?xml version=’1.0′ encoding=’UTF-8′?>";

  strXML += "<files>";

  for ( i = 0 ; i < size; i++)
   strXML += "<file name=’file" + i + ".html’ ></file>";

  strXML += "</files>";
  return strXML;
}

function parseJSON(str)
{
  data =  eval(’(’ + str + ‘)’);
  out(data.length + " data parsed");  
}

function parseXML(str)
{
  var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
  xmlDoc.loadXML(str);

  if (xmlDoc.documentElement!=null)
  {
    var files = xmlDoc.documentElement.childNodes;
    out(files.length + " data parsed");
  }
  else
  {
    out("Invalid XML parsed");
  }
}

function testJSON()
{
  var datasize = document.getElementById("datasize").value;
  out("generating JSON for test…");

  str = generateJSON(datasize);

  out("begin parse JSON…");

  var begin = new Date();
  parseJSON(str);

  var end = new Date();
  out("Parse JSON data with " + datasize + " records, used time: " + (end - begin) + "ms");

}

function testXML()
{

  var datasize = document.getElementById("datasize").value;

  out("generating XML for test…");
  var strXML = generateXML(datasize);

  out("begin parse XML…");

  var begin = new Date();
  parseXML(strXML);

  var end = new Date();
  out("Parse XML data with " + datasize + " records, used time: " + (end - begin) + "ms");

}

</script>
<head>

<body>

Lines of data: <input type="edit" value="100" id="datasize"></input>
<input type="button" onclick="testJSON()" value="Test with JSON"></input>
<input type="button"  onclick="testXML()" value="Test with XML"></input>

<hr/>

<div id="output">
</div>
</body>
</html>

Here is a result:

generating JSON for test…
begin parse JSON…
10000 data parsed
Parse JSON data with 10000 records, used time: 31ms
generating XML for test…
begin parse XML…
10000 data parsed
Parse XML data with 10000 records, used time: 16ms

generating JSON for test…
begin parse JSON…
20000 data parsed
Parse JSON data with 20000 records, used time: 78ms
generating XML for test…
begin parse XML…
20000 data parsed
Parse XML data with 20000 records, used time: 47ms

Feedback are welcome…

Popularity: 15% [?]

Top 10 js sites

Thursday, September 20th, 2007
    1. Top 10 custom JavaScript functions of all time
    2. Top 10 Web Developer Libraries
    3. Top 10 Ajax Applications
    4. Top 10 Ajax Tutorials for Beginners
    5. Top 10 Most Useful JavaScripts
    6. Top 10 JavaScript String Extensions
    7. Top 10 Debugging & testing Javascript
    8. Top 10 JavaScript Books
    9. Top 10 Javascript Tools Everyone Should Have
    10. Top 10 Tips To A Better Form

via this site

Popularity: 13% [?]

Google Maps + Google Ditu Mashup

Sunday, July 22nd, 2007

转到 Google 地图 主页Go to Google Maps Home

Some days ago I wrote a small bookmarklet to switch between Google Maps and Google Ditu, now I have a simple maskup which add google ditu as a customized layer in Google Maps.

The reason why Google Ditu in China can’t contain a satellite layer is the goverment’s policy. There are so many unbelieveable policies here in China, this one is just a simple example.

The satellite images and the maps don’t match well, actually that’s also a policy and requirement by China goverment… :(

 

Here is page with the code,use browser’s “view source” to see how this implemented.

Popularity: 15% [?]

Get google reader’s UID

Sunday, July 22nd, 2007

 

Playing with Google Reader API, in some API it need a UID.

Google Reader users are assigned a 20-digit user ID used throughout Google’s feed system. No cookies or session IDs are required to access this member-specific data. User-specifc data is accessible using the google.com cookie named “SID.”

Open Google reader, in address bar, input following and hit return:

javascript:if (prompt(”Google Reader ID”, _USER_ID)) {};

What you get a 20 digits, that’s google reader’s UID, and can be used in Google Reader API.

Unfortunately, many of the APIs only return a 500 internal error page. :( 

Popularity: 11% [?]

Google Maps / Ditu switcher bookmarklet

Wednesday, July 18th, 2007

Ditu.google.com is the Chinese version of Google Maps, Google maps is a great product however in China, there is no detailed map, all we can play around is the satellite images. Google China released Ditu which is the answer, however due to China’s local law google is not allowed to put it together with the satellite image.

I wrote a small bookmarklet to allow you siwtch between Ditu and Google Maps automatically, locate to a position either in Google Maps or Ditu, click the bookmarlet, the page will automatically switch to the same location of the other site.

Drag this link to your Firefox address bar:

Ditu/Maps Switcher

The source code is here:

<a href="javascript:url = ''+document.getElementById('link'); if ((url.search(/http:\/\/maps.goog/i) ==-1) && (url.search(/http:\/\/ditu.goog/i)==-1)) { alert('Do not click this links, drag it too your address bar. \nThis bookmarklet only works on maps.google.com or ditu.google.com.\nCheck update on http://dev.robertmao.com/category/google-maps for latest updates or other google maps hack.'); } else { if (url.search(/ditu/i)==-1) { url=url.replace(/maps./i, 'ditu.'); } else {url=url.replace(/ditu./i, 'maps.'); if (url.search(/&t=k/i) ==-1) url += '&t=k'; } window.location=url;}">Ditu/Maps Switcher</a>

You can also copy the part “javascript:….url;}” and paste in your address input box and hit return to make a switch. However I think drag the link as a bookbarklet is a better idea.

I didn’t test under IE, so not sure if it work. Try at your own risk.

I am thinking writing a hack to overlap the image files of Ditu and Google Maps, it gonna be cooler, but this is still just an idea. One issue is, during I play arround, I found Ditu’s position is not exactly match the satellites images, maybe it’s also because google need to follow some local policy.

Popularity: 14% [?]

Some XSS Vulnerabilities links

Monday, July 16th, 2007

I spent some time on XSS (Cross Site Scripting) Vulnerabilities  scan and problem solving last month, here is some links when I used, if I have time I will write more on this topic.

A detail presentation on how a google desktop’s security hole was used by XSS to perform a “perfect attack”: http://download.watchfire.com/googledesktopdemo/index.htm

A good slides explain what’s XSS and how it attack in action: http://www.sven.de/xsss/wth_xsss_slides.pdf

XSS explained and free scan service: http://www.acunetix.com/websitesecurity/cross-site-scripting.htm

Scripts for XSS hack (don’t use it do bad things…): http://ha.ckers.org/xss.html

Step by step hack a site with XSS (don’t use it do bad things, again…): http://ha.ckers.org/deathby1000cuts/

A very good article: http://www.milw0rm.com/papers/162

Popularity: 11% [?]

Close
E-mail It
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.