diff --git a/lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js b/lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js index c7478bb..5bd80bc 100644 --- a/lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js +++ b/lib/tools/phantomas/custom_modules/core/scopeYLT/scopeYLT.js @@ -42,7 +42,7 @@ exports.module = function(phantomas) { return false; } - phantomas.log('Attaching a spy to "' + fn + '" function...'); + phantomas.log('Attaching a YLT spy to "' + fn + '" function...'); obj[fn] = function() { var result; diff --git a/lib/tools/phantomas/custom_modules/modules/javaScriptBottleYLT/javaScriptBottleYLT.js b/lib/tools/phantomas/custom_modules/modules/javaScriptBottleYLT/javaScriptBottleYLT.js new file mode 100644 index 0000000..3409c04 --- /dev/null +++ b/lib/tools/phantomas/custom_modules/modules/javaScriptBottleYLT/javaScriptBottleYLT.js @@ -0,0 +1,56 @@ +/** + * Reports the use of functions known to be serious performance bottlenecks in JS + * + * @see http://www.nczonline.net/blog/2013/06/25/eval-isnt-evil-just-misunderstood/ + * @see http://www.quirksmode.org/blog/archives/2005/06/three_javascrip_1.html + * @see http://www.stevesouders.com/blog/2012/04/10/dont-docwrite-scripts/ + */ +/* global document: true, window: true */ +'use strict'; + +exports.version = '0.1'; + +exports.module = function(phantomas) { + phantomas.setMetric('documentWriteCalls'); //@desc number of calls to either document.write or document.writeln + phantomas.setMetric('evalCalls'); // @desc number of calls to eval (either direct or via setTimeout / setInterval) + + phantomas.once('init', function() { + phantomas.evaluate(function() { + (function(phantomas) { + function report(msg, caller, backtrace, metric) { + phantomas.log(msg + ': from ' + caller + '!'); + phantomas.log('Backtrace: ' + backtrace); + phantomas.incrMetric(metric); + } + + // spy calls to eval() + phantomas.spy(window, 'eval', function(code) { + report('eval() called directly', phantomas.getCaller(), phantomas.getBacktrace(), 'evalCalls'); + phantomas.log('eval\'ed code: ' + (code || '').substring(0, 150) + '(...)'); + }); + + // spy calls to setTimeout / setInterval with string passed instead of a function + phantomas.spy(window, 'setTimeout', function(fn, interval) { + if (typeof fn !== 'string') return; + + report('eval() called via setTimeout("' + fn + '")', phantomas.getCaller(), phantomas.getBacktrace(), 'evalCalls'); + }); + + phantomas.spy(window, 'setInterval', function(fn, interval) { + if (typeof fn !== 'string') return; + + report('eval() called via setInterval("' + fn + '")', phantomas.getCaller(), phantomas.getBacktrace(), 'evalCalls'); + }); + + // spy document.write(ln) + phantomas.spy(document, 'write', function(arg) { + report('document.write() used', phantomas.getCaller(), phantomas.getBacktrace(), 'documentWriteCalls'); + }); + + phantomas.spy(document, 'writeln', function(arg) { + report('document.writeln() used', phantomas.getCaller(), phantomas.getBacktrace(), 'documentWriteCalls'); + }); + })(window.__phantomas); + }); + }); +}; diff --git a/lib/tools/phantomas/phantomasWrapper.js b/lib/tools/phantomas/phantomasWrapper.js index 4954096..4515a60 100644 --- a/lib/tools/phantomas/phantomasWrapper.js +++ b/lib/tools/phantomas/phantomasWrapper.js @@ -37,6 +37,7 @@ var PhantomasWrapper = function() { 'eventListeners', // overridden 'filmStrip', // not needed 'har', // not needed for the moment + 'javaScriptBottlenecks', // needs to be launched after custom module scopeYLT 'pageSource', // not needed 'screenshot', // not needed for the moment 'waitForSelector', // not needed