[Greasemonkey] Patterns

Edward Lee edilee at gmail.com
Tue Apr 12 21:08:49 EDT 2005


Testing on a page with a lot of 10 spans per line x 10 lines x group
of 10 in 10 divs.. But then again, that isn't quite a normal page ;)

> document.getElementsByTagName('span').length
10000

function timeIt(num, fun) {
  var start = new Date().getTime();
  for (var i = 0; i < num; i++) {
    fun();
  }
  var end = new Date().getTime();

  return end - start; // timediff in milliseconds
}

> timeIt(100000, function() { document.getElementsByTagName('span') })
2735

> timeIt(100, function() { document.evaluate("//span", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null) })
1453

> timeIt(100, function() { document.evaluate("//span", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) })
1469

> timeIt(1000, function() { document.evaluate("/html/body/div/span", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null) })
5687

On greasemonkey.mozdev.org..

> timeIt(100000, function() { document.getElementsByTagName('a') })
2797

> timeIt(10000, function() { document.evaluate("//a", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null) })
3562

> timeIt(10000, function() { document.evaluate("//a", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) })
3593

So yes, xPath is slower by quite a bit, but in most usage it probably
won't be noticed. Plus you can apply a lot more specification when
selecting nodes with xPath.

-- 
Ed


More information about the Greasemonkey mailing list