├── index.js ├── package.json ├── .gitignore ├── LICENSE └── README.md /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (content) { 2 | this.cacheable() 3 | var start = this.resource + '--start' 4 | var end = this.resource + '--end' 5 | return ( 6 | '\n' + 7 | 'try { window.performance.mark(\'' + start + '\') } catch (e) {};\n' + 8 | content + '\n' + 9 | 'try { window.performance.mark(\'' + end + '\');\n' + 10 | 'window.performance.measure(\'' + this.resource + '\', \'' + start + '\', \'' + end + '\')\n' + 11 | '} catch (e) {};' 12 | ) 13 | } 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "performance-loader", 3 | "version": "1.0.0", 4 | "description": "performance-loader for webpack", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/maxkostow/performance-loader.git" 12 | }, 13 | "keywords": [ 14 | "webpack", 15 | "performance" 16 | ], 17 | "author": "Max Kostow", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/maxkostow/performance-loader/issues" 21 | }, 22 | "homepage": "https://github.com/maxkostow/performance-loader#readme" 23 | } 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Max Kostow 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # performance-loader 2 | 3 | Wraps modules in the browser performance API. 4 | 5 | ## Installation 6 | 7 | `npm install performance-loader --save-dev` 8 | 9 | ## Usage 10 | 11 | [Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html) 12 | 13 | ### Example config 14 | 15 | ``` javascript 16 | module.exports = { 17 | module: { 18 | postLoaders: [ 19 | { loader: "performance-loader" }, 20 | ] 21 | } 22 | }; 23 | ``` 24 | 25 | ### In browser 26 | 27 | After initializing the page in a browser try: 28 | 29 | ```javascript 30 | console.table( 31 | window.performance.getEntriesByType('measure') 32 | .filter(p => p.duration > 1) // only modules that took longer than 1ms 33 | .sort((a, b) => b.duration - a.duration) // sort by descending duration 34 | .map(p => [p.name, p.duration]) // map for the table 35 | ) 36 | // table of modules that took longer than 1ms to run 37 | ``` 38 | 39 | ## How it works 40 | 41 | It wraps your modules in [`performance.mark`](https://developer.mozilla.org/en-US/docs/Web/API/Performance/mark) and then calls [`performance.measure`](https://developer.mozilla.org/en-US/docs/Web/API/Performance/measure). 42 | 43 | ```javascript 44 | window.performance.mark('/path/to/module.js--start') 45 | /*** module.js code here ***/ 46 | window.performance.mark('/path/to/module.js--end') 47 | window.performance.measure('/path/to/module.js', '/path/to/module.js--start', '/path/to/module.js--end') 48 | ``` 49 | --------------------------------------------------------------------------------