Coder Social home page Coder Social logo

code-snippets's Introduction

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

code-snippets's People

Contributors

argshook avatar bahmutov avatar rvaneetvelde avatar sgtpooki avatar stevermeister avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

code-snippets's Issues

update-code-snippets.js not working in Chrome 45.0.2454.101

I'm getting the following error when running the snippet update-code-snippets.js to update my snippets:
Cannot find scriptSnippets, are you running in the secondary DevTools? see https://github.com/bahmutov/code-snippets#updating-local-code-snippets

My Chrome version is 45.0.2454.101 m.

I ran the snippet from the 2nd DevTools instance (following the steps in documentation) but it doesn't work - scriptSnippets key doesn't exist in localStorage. It seems that scriptSnippets are no longer stored in localStorage?

My temporary workaround to this problem is to modify the update-code-snippet.js (not nice but works for me atm):
https://gist.github.com/jchen86/3ca08af721a43725ebf6

snippets are no longer available via localStorage

We made snippets (among other settings) available from incognito. I think this change landed in Chrome stable around 46/47 or so.

As a result, they are no longer available in localStorage. Here's the new technique. These are run from a devtools-on-devtools window.

getting snippets

InspectorFrontendHost.getPreferences(_ => console.log(JSON.parse(_.scriptSnippets)))

setting snippets

InspectorFrontendHost.setPreference("scriptSnippets", JSON.stringify(yourSnippets))

FWIW, InspectorFrontendHost.getPreferences will return everything that has migrated to the incognito-available settings (most things), while a few items (like breakpoints) remain in the devtools' localStorage

Tagging a few other issues that use this technique:

Refused to load the script

I tried to run code snippet and it gave me:

Refused to load the script 'https://rawgit.com/bahmutov/code-snippets/master/first-paint.js' because it violates the following Content Security Policy directive: "script-src assets-cdn.github.com collector-cdn.github.com".

console.time is not as accurate as performance.now

Run this in your console and see for yourself. The difference is minor, but it is there.

(function(){
    var now;
    var total;
    for(var i = 0; i < 10; i++) {
        console.time('test');
        console.timeEnd('test');

        now = performance.now();
        total = performance.now()-now;
        console.log(total);
    }
})();

Typo or missing word in the README.md file.

In the "Updating local code snippets" section, the second sentence says:

"Create a code snippets and copy source from update-code-snippets.js."

Is this a typo, or is there a missing word after snippets?

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.