profile.getelement.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //node --prof --prof_auto profile.getelement.js
  2. //deps/v8/tools/mac-tick-processor v8.log > profile.getelement.txt
  3. var sys = require("sys");
  4. var fs = require("fs");
  5. var htmlparser = require("./node-htmlparser");
  6. var htmlparser_old = require("./node-htmlparser.old");
  7. var testIterations = 100; //Number of test loops to run
  8. function getMillisecs () {
  9. return((new Date()).getTime());
  10. }
  11. function timeExecutions (loops, func) {
  12. var start = getMillisecs();
  13. while (loops--)
  14. func();
  15. return(getMillisecs() - start);
  16. }
  17. var html = fs.readFileSync("testdata/getelement.html");
  18. var handler = new htmlparser.DefaultHandler(function(err, dom) {
  19. if (err)
  20. sys.debug("Error: " + err);
  21. });
  22. var parser = new htmlparser.Parser(handler);
  23. parser.parseComplete(html);
  24. var dom = handler.dom;
  25. //sys.debug(sys.inspect(dom, false, null));
  26. sys.puts("New: " + timeExecutions(testIterations, function () {
  27. var foundDivs = htmlparser.DomUtils.getElementsByTagName("div", dom);
  28. // sys.puts("Found: " + foundDivs.length);
  29. var foundLimitDivs = htmlparser.DomUtils.getElementsByTagName("div", dom, null, 100);
  30. // sys.puts("Found: " + foundLimitDivs.length);
  31. var foundId = htmlparser.DomUtils.getElementById("question-summary-3018026", dom);
  32. // sys.puts("Found: " + foundId);
  33. }));
  34. sys.puts("Old: " + timeExecutions(testIterations, function () {
  35. var foundDivs = htmlparser_old.DomUtils.getElementsByTagName("div", dom);
  36. // sys.puts("Found: " + foundDivs.length);
  37. // var foundLimitDivs = htmlparser.DomUtils.getElementsByTagName("div", dom);
  38. // sys.puts("Found: " + foundLimitDivs.length);
  39. var foundId = htmlparser_old.DomUtils.getElementById("question-summary-3018026", dom);
  40. // sys.puts("Found: " + foundId);
  41. }));