javaScriptBottleYLT.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Reports the use of functions known to be serious performance bottlenecks in JS
  3. *
  4. * @see http://www.nczonline.net/blog/2013/06/25/eval-isnt-evil-just-misunderstood/
  5. * @see http://www.quirksmode.org/blog/archives/2005/06/three_javascrip_1.html
  6. * @see http://www.stevesouders.com/blog/2012/04/10/dont-docwrite-scripts/
  7. */
  8. /* global document: true, window: true */
  9. exports.version = '0.1.a';
  10. exports.module = function(phantomas) {
  11. 'use strict';
  12. phantomas.setMetric('documentWriteCalls'); //@desc number of calls to either document.write or document.writeln
  13. phantomas.setMetric('evalCalls'); // @desc number of calls to eval (either direct or via setTimeout / setInterval)
  14. phantomas.once('init', function() {
  15. phantomas.evaluate(function() {
  16. (function(phantomas) {
  17. function report(msg, caller, backtrace, metric) {
  18. phantomas.log(msg + ': from ' + caller + '!');
  19. phantomas.log('Backtrace: ' + backtrace);
  20. phantomas.incrMetric(metric);
  21. }
  22. // spy calls to eval()
  23. /*phantomas.spy(window, 'eval', function(code) {
  24. report('eval() called directly', phantomas.getCaller(), phantomas.getBacktrace(), 'evalCalls');
  25. phantomas.log('eval\'ed code: ' + (code || '').substring(0, 150) + '(...)');
  26. });*/
  27. // spy calls to setTimeout / setInterval with string passed instead of a function
  28. /*phantomas.spy(window, 'setTimeout', function(fn, interval) {
  29. if (typeof fn !== 'string') return;
  30. report('eval() called via setTimeout("' + fn + '")', phantomas.getCaller(), phantomas.getBacktrace(), 'evalCalls');
  31. });*/
  32. /*phantomas.spy(window, 'setInterval', function(fn, interval) {
  33. if (typeof fn !== 'string') return;
  34. report('eval() called via setInterval("' + fn + '")', phantomas.getCaller(), phantomas.getBacktrace(), 'evalCalls');
  35. });*/
  36. // spy document.write(ln)
  37. phantomas.spy(document, 'write', function(arg) {
  38. report('document.write() used', phantomas.getCaller(), phantomas.getBacktrace(), 'documentWriteCalls');
  39. });
  40. phantomas.spy(document, 'writeln', function(arg) {
  41. report('document.writeln() used', phantomas.getCaller(), phantomas.getBacktrace(), 'documentWriteCalls');
  42. });
  43. })(window.__phantomas);
  44. });
  45. });
  46. };