Chrome DevTools code snippets
JavaScript HTML
Latest commit 713b144 Nov 3, 2016 @bahmutov fix(readme): readme links
Permalink
Failed to load latest commit information.
images Updating local code snippets from latest github repo, fixes #6 Jan 23, 2015
test Added method to count number of digest cycles Mar 20, 2015
utils feat(dom): added snippet to clean up the page Feb 29, 2016
.gitignore adding linting via grunt Dec 21, 2014
.npmrc added npm rc Nov 18, 2015
.travis.yml feat(release): setup semantic release, fixes #17 Feb 29, 2016
Gruntfile.js moved lint config files to utils folder Jan 23, 2015
README.md fix(readme): readme links Nov 3, 2016
boilerplate.js remote example Oct 21, 2014
bower.json feat(release): setup semantic release, fixes #17 Feb 29, 2016
css-layout.js chore(lint): linting JS Nov 3, 2016
expensive-keys.js if single object is passed, it will be used as array Feb 4, 2015
find-expensive-digest.png added find expensive digest screenshot Nov 10, 2014
first-paint-code-snippet-remote.png remote example Oct 21, 2014
first-paint-code-snippet.png initial Oct 21, 2014
first-paint-remote.js remote example Oct 21, 2014
first-paint.js fixed jshint Dec 22, 2014
github-pull-request-template.js docs(script): added source link to github pull request snippet Nov 30, 2015
harlem-shake-xss.js chore(lint): linting JS Nov 3, 2016
keys-vs-values.js added keys-vs-values.js Dec 2, 2014
local-storage-size.js fixed jscs Dec 22, 2014
ng-count-digest-cycle-simple.js Added simple digest counter Oct 16, 2015
ng-count-digest-cycles.js just printing number of digest cycles Mar 25, 2015
ng-count-watchers.js feat(ng): counting angular watchers in a tree Jan 4, 2016
ng-find-expensive-digest.js fixed eslint Dec 22, 2014
ng-find-scope-property.js fixed jscs Dec 22, 2014
ng-idle-apply-timing.js fix(idle): using explicit injection for idle timing script Nov 30, 2015
ng-monitor-digest-cycle.js switched to counting digest cycle instead of monitoring Aug 12, 2015
ng-profile-data-change.js fixed some jshint issues Dec 22, 2014
ng-profile-local-digest.js ngProfileLocalDigest uses performance.now Feb 19, 2015
ng-profile-scope-method.js query scope method by selector Oct 18, 2015
ng-run-digest-cycle.js adding linting via grunt Dec 21, 2014
ng-scope-size.js fixed jscs Dec 22, 2014
ng-throw-error.js added throwing error from angular digest cycle Aug 27, 2015
package.json feat(release): setup semantic release, fixes #17 Feb 29, 2016
profile-method-call.js better profiling messages Feb 4, 2015
profile-prototype-method.js added prototype method profiling snippet Apr 20, 2015
profile-separate-calls.js added profile separate calls method Apr 21, 2015
remove-all-but.js feat(remove): remove elements using querySelectorAll Mar 1, 2016
test-script-injection.js feat(script): added testing external script injection Dec 10, 2015
time-method-call.js better profiling messages Feb 4, 2015
timing.js initial Oct 21, 2014
update-code-snippets.js better error message when fails to update snippets Feb 4, 2015

README.md

Chrome DevTools code snippets

Performance, debugging and testing code snippets to be run in Chrome DevTools

NPM

Build status dependencies devdependencies Codacy Badge semantic-release

fist paint

Read Code Snippets tutorial, Performance profiling using DevTools code snippets and How to improve Angular application performance using code snippets.

Note: code snippets do NOT have access to the full console API, for example no access to console.monitor.

Snippets

Security

DOM and CPU generic performance

Storage measurements

Angular performance

  • ng-count-watchers.js - counts total watchers in the page. More watchers - slower digest cycle.
  • ng-idle-apply-timing.js - measures how long a digest cycle takes without any data changes. This measures purely how long all watched expressions take to compute and compare to previous values (dirty checking).
  • ng-profile-scope-method.js - installs profile calls around a given scope method. When the method completes, the original non-instrumented version will be restored. The browser will have timeline and CPU profile.
  • ng-run-digest-cycle.js - triggers digest cycle starting with root scope.
  • ng-profile-data-change.js - changes data on the scope, runs digest cycle to profile listeners.
  • ng-scope-size.js - finds total size of all user objects attached to all scopes. Smaller data - faster copying and comparison during digest cycle.
  • ng-find-scope-property.js - finds all scopes that own a property with given name.
  • ng-profile-local-digest.js - runs idle digest cycle starting at the scope that surrounds given selector. Useful to find parts of the page with expensive watchers.
  • ng-find-expensive-digest.js builds upon ng-profile-local-digest.js to measure digest duration for several selectors and print sorted table starting with the slowest digest duration.
  • ng-monitor-digest-cycle.js - prints a string every time a digest cycle runs.
  • ng-count-digest-cycles.js - counts number of full digest cycles (from the root scope) that run when a scope method executes. Useful because sometimes you can get away with just a local digest cycle, rather than a full update. See Local Angular scopes.
  • ng-count-digest-cycle-simple.js - keeps counting number of times the digest cycle runs.
  • ng-throw-error.js throws an error from the digest cycle; useful for checking if your exception handler is working.

Misc snippets

All snippets, including mine are distributed under MIT license.

Updating local code snippets

You can update local code snippets by downloading new versions from this github repository. Create a new code snippet and copy the source from update-code-snippets.js.

Note: the approach below does not work any more, see the open issue.

You will run this code snippet in an unusual way. First, open any web page, even an empty tab. Open the DevTools in undocked mode (Command+Option+I on Mac). Then open the DevTools again, while focused on the first DevTools. This will open the second DevTools instance with the source for the first DevTools panels. If you inspect the localStorage variable in the second DevTools window, you will find lots of interesting stuff, including all the code snippets in the localStorage.scriptSnippets property.

Whenever you want to update the your local code snippets in the Chrome DevTools, execute the update-code-snippets.js snippet in the second DevTools instance. The update script looks at the your current code snippets and tries to download a file with same name from the code snippets github repository (via RawGit). If the remote file has been downloaded successfully, it will replace the snippet. After all snippets are checked, reopen the DevTools to load the updated source code.

update code snippets

Note, that only the latest source is downloaded, not any particular release. Also, only code snippets with names matching existing files in this repo are replaced. If you do not want to override a code snippet - just rename it, for example, remove the .js extension.

Remote download a single script

You can download and run a single snippet by using the following boilerplate (scripts are via downloaded via RawGit)

(function firstPaintRemote() {
  // form rawGit proxy url
  var ghUrl = 'bahmutov/code-snippets/master/first-paint.js';
  var rawUrl = 'https://rawgit.com/' + ghUrl;
  // download and run the script
  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = rawUrl;
  head.appendChild(script);
}());

remote

Small print

Author: Gleb Bahmutov © 2014

License: MIT - do anything with the code, but don't blame me if it does not work.

Spread the word: tweet, star on github, etc.

Support: if you find any problems with this module, email / tweet / open issue on Github