• This repository has been archived on 03/May/2019
  • Stars
    star
    657
  • Rank 66,218 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Little utility to show stats about your page's angular digest/watches.

ng-stats

unmaintained

npm version npm downloads

Little utility to show stats about your page's angular digest/watches. This library currently has a simple script to produce a chart (see below). It also creates a module called angularStats which has a directive called angular-stats which can be used to put angular stats on a specific place on the page that you specify.

Example Green (digests are running smoothly):

Example Green

Example Red (digests are taking a bit...):

Example Red

Interactive Demo

The first number is the number of watchers on the page (including {{variables}}, $scope.$watch, etc.). The second number is how long (in milliseconds) it takes angular to go through each digest cycle on average (bigger is worse). The graph shows a trend of the digest cycle average time.

Thanks

Viper Bailey for writing the initial version (and most of the graph stuff).

Development

  1. npm install
  2. bower install
  3. grunt for server
  4. grunt release for release

Installation

Bookmarklet

Copy the code below and create a bookmarklet for ng-stats to use it on any angular website (so long as the debug info is enabled, if not, you'll need to run angular.reloadWithDebugInfo() first).

javascript: (function() {var a = document.createElement("script");a.src = "https://rawgit.com/kentcdodds/ng-stats/master/dist/ng-stats.js";a.onload=function(){window.showAngularStats()};document.head.appendChild(a)})();

If you just want the chart for development purposes, it's actually easiest to use as a Chrome DevTools Snippet. Just copy/paste the dist/ng-stats.js file into a snippet.

However, it uses UMD, so you can also include it in your app if you want via:

$ npm|bower install ng-stats

or download dist/ng-stats.js and

<script src="path-to/ng-stats.js"></script>

or

var showAngularStats = require('path-to-ng-stats');

You now have a angularStats module and showAngularStats function you can call

Chart

Usage

Simply invoke showAngularStats( { options } ) and the chart will appear. It also returns an object with a few handy things depending on your options. One of these things is listeners which is an object that has two objects: digestLength and watchCount. You can add a custom listener that is called when the digest cycles happen (though for performance reasons when calculating the watchCount, the watchCount listeners are throttled). Here's an example of adding custom listeners:

var ngStats = showAngularStats();

ngStats.listeners.digestLength.nameOfYourListener = function(digestLength) {
  console.log('Digest: ' + digestLength);
};

ngStats.listeners.watchCount.nameOfYourListener = function(watchCount) {
  console.log('Watches: ' + watchCount);
};

Options

You can pass the function one (optional) argument. If you pass false it will turn off "autoload" and do nothing. You can also pass an object with other options:

position (object) - default: 'topleft'

Controls the position of the graphic. Possible values: Any combination of top, left, right, bottom.

digestTimeThreshold (number) - default: 16

The time (in milliseconds) where it goes from red to green.

autoload (string or boolean) - default: false

Uses the Storage API to store whether the graphic should be automatically loaded every time the page is reloaded. Pass in 'localStorage' for persistent loading or 'sessionStorage' to load ng-stats for only the current session.

Note, if you pass false as options, it will simply remove the stats window and exit: showAngularStats(false)

trackDigest (boolean) - default: false

showAngularStats returns an object. Setting this to true will add an array to that object called digest that holds all of the digest lengths.

trackWatches (boolean) - default: false

showAngularStats returns an object. Setting this to true will add an array to that object called watches that holds all of the watch counts as they change.

logDigest (boolean) - default: false

Setting this to true will cause ng-stats to log out the digest lengths to the console. It will be colored green or red based on the digestTimeThreshold.

logWatches (boolean) - default: false

Setting this to true will cause ng-stats to log out the watch count to the console as it changes.

htmlId (string) - default: null

Sets an HTML ID attribute to the rendered stats element.

rootScope (object) - default: undefined

Passes the $rootScope to ng-stats. This parameter is only required for Ionic support where the ng-scope and ng-isolate-scope classes are removed. The only way of using the ng-stats with Ionic is invoking showAngularStats( { options } ) in your code and passing the $rootScope manually.

Module

Simply declare it as a dependency angular.module('your-mod', ['angularStats']);

Then use the directive:

<div angular-stats watch-count=".watch-count" digest-length=".digest-length"
     on-watch-count-update="onWatchCountUpdate(watchCount)"
     on-digest-length-update="onDigestLengthUpdate(digestLength)">
  Watch Count: <span class="watch-count"></span><br />
  Digest Cycle Length: <span class="digest-length"></span>
</div>

angular-stats attributes

angular-stats

The directive itself. No value is expected

watch-count

Having this attribute will keep track of the watch count and update the text of a specified element. Possible values are:

  1. Selector for a child element to update
  2. no value - refers to the current element (updates the text of the current element)

watch-count-root

angular-stats defaults to keeping track of the watch count for the whole page, however if you want to keep track of a specific element (and its children), provide this with a element query selector. As a convenience, if this is provided then the watch-count-root will be set to the element itself. Also, if you want to scope the query selector to the element, add watch-count-of-child as an attribute (no value)

on-watch-count-update

Because of the performance implications of calculating the watch count, this is not called every digest but a maximum of once every 300ms. Still avoid invoking another digest here though. The name of the variable passed is watchCount (like you see in the example).

digest-length

This works similar to the watch-count attribute. It's presence will cause the directive to keep track of the digest-length and will update the text of a specified element (rounds to two decimal places). Possible values are:

  1. Selector for a child element to update
  2. no value - refers to the current element (updates the text of the current element)

on-digest-length-update

Pass an expression to evaluate with every digest length update. This gets called on every digest (so be sure you don't invoke another digest in this handler or you'll get an infinite loop of doom). The name of the variable passed is digestLength (as in the example).

Roadmap

  • Add analysis to highlight areas on the page that have highest watch counts.
  • Somehow find out which watches are taking the longest... Ideas on implementation are welcome...
  • See what could be done with the new scoped digest coming in Angular version 1.3.
  • Count the number of digests or provide some analytics for frequency?
  • Create a Chrome Extension for the chart or integrate with batarang?
  • Other ideas?

Other notes

Performance impact

This will not impact the speed of your application at all until you actually use it. It also will hopefully only negatively impact your app's performance minimally. This is intended to be used in development only for debugging purposes so it shouldn't matter much anyway. It should be noted that calculating the watch count can be pretty expensive, so it's throttled to be calculated a minimum of 300ms.

Using in an iframe

Thanks to this brilliant PR from @jinyangzhen, you can run ng-stats in an iframe (like plunker!). See the PR for an example of how to accomplish this.

License

MIT

More Repositories

1

cross-env

🔀 Cross platform setting of environment scripts
JavaScript
6,240
star
2

match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaScript
TypeScript
3,616
star
3

advanced-react-patterns

This is the latest advanced react patterns workshop
JavaScript
2,885
star
4

babel-plugin-macros

🎣 Allows you to build simple compile-time libraries
JavaScript
2,605
star
5

react-hooks

Learn React Hooks! 🎣 ⚛
JavaScript
2,550
star
6

bookshelf

Build a ReactJS App workshop
JavaScript
2,533
star
7

kentcdodds.com

My personal website
MDX
2,143
star
8

use-deep-compare-effect

🐋 It's react's useEffect hook, except using deep comparison on the inputs, not reference equality
TypeScript
1,726
star
9

mdx-bundler

🦤 Give me MDX/TSX strings and I'll give you back a component you can render. Supports imports!
JavaScript
1,702
star
10

react-performance

Let's make our apps fast ⚡
JavaScript
1,557
star
11

advanced-react-patterns-v2

Created with CodeSandbox
JavaScript
1,499
star
12

testing-workshop

A workshop for learning how to test JavaScript applications
JavaScript
1,363
star
13

babel-plugin-preval

🐣 Pre-evaluate code at build-time
TypeScript
1,349
star
14

advanced-react-patterns-v1

The course material for my advanced react patterns course on Egghead.io
HTML
1,092
star
15

react-testing-library-course

Test React Components with Jest and React Testing Library on TestingJavaScript.com
JavaScript
1,004
star
16

testing-react-apps

A workshop for testing react applications
JavaScript
977
star
17

kcd-scripts

CLI toolbox for common scripts for my projects
JavaScript
870
star
18

stop-runaway-react-effects

🏃 Catches situations when a react use(Layout)Effect runs repeatedly in rapid succession
JavaScript
788
star
19

netlify-shortener

Your own free URL shortener with Netlify
JavaScript
778
star
20

beginners-guide-to-react

The Beginner's Guide To ReactJS
HTML
757
star
21

react-suspense

React Suspense workshop
JavaScript
746
star
22

old-kentcdodds.com

Kent's Homepage
JavaScript
734
star
23

dotfiles

Shell
510
star
24

js-testing-fundamentals

Fundamentals of Testing in JavaScript on TestingJavaScript.com
JavaScript
500
star
25

react-toggled

Component to build simple, flexible, and accessible toggle components
JavaScript
453
star
26

jest-cypress-react-babel-webpack

Configure Jest for Testing JavaScript Applications and Install, Configure, and Script Cypress for JavaScript Web Applications on TestingJavaScript.com
JavaScript
442
star
27

advanced-remix

TypeScript
393
star
28

react-testing-library-examples

Created with CodeSandbox
HTML
380
star
29

testing-node-apps

Test Node.js Backends on TestingJavaScript.com
JavaScript
365
star
30

es6-workshop

A very hands on 👐 workshop 💻 about ES6 and beyond.
JavaScript
362
star
31

es6-todomvc

The vanillajs example converted to es6
JavaScript
353
star
32

babel-plugin-codegen

💥 Generate code at build-time
TypeScript
345
star
33

eslint-config-kentcdodds

ESLint configuration for projects that I do... Feel free to use this!
JavaScript
332
star
34

cloc

An npm module for distributing cloc by Al Danial
JavaScript
325
star
35

asts-workshop

Improved productivity 💯 with the practical 🤓 use of the power 💪 of Abstract Syntax Trees 🌳 to lint ⚠️ and transform 🔀 your code
JavaScript
295
star
36

how-jest-mocking-works

JavaScript
294
star
37

js-mocking-fundamentals

JavaScript Mocking Fundamentals on TestingJavaScript.com
JavaScript
281
star
38

webpack-config-utils

Utilities to help your webpack config be easier to read
JavaScript
262
star
39

express-app-example

How I structure Express Apps (example repo)
JavaScript
261
star
40

dom-testing-library-with-anything

Use DOM Testing Library to test any JS framework on TestingJavaScript.com
JavaScript
217
star
41

learn-react

Learn React with a laser focused, guided approach.
JavaScript
213
star
42

the-webs-next-transition

TypeScript
211
star
43

modern-react

workshop about React's hottest new features in 16.7.0
JavaScript
207
star
44

react-jest-workshop

JavaScript
199
star
45

react-github-profile

JavaScript
199
star
46

react-ava-workshop

🐯 A workshop repository for testing React ⚛ with AVA 🚀 --> slides
JavaScript
192
star
47

api-check

VanillaJS version of ReactJS propTypes
JavaScript
191
star
48

starwars-names

Get a random Star Wars name
JavaScript
185
star
49

import-all.macro

A babel-macro that allows you to import all files that match a glob
JavaScript
177
star
50

remix-todomvc

An Implementation of TodoMVC with Remix
TypeScript
172
star
51

rtl-css-js

RTL for CSS in JS
JavaScript
161
star
52

react-workshop-app

An abstraction for all my React workshops
TypeScript
144
star
53

generator-kcd-oss

A yeoman generator for my open source modules
JavaScript
140
star
54

remix-workshop

TypeScript
133
star
55

issue-template

A way for github projects to make templates for github issues.
JavaScript
131
star
56

react-hooks-and-suspense-egghead-playlist

This is the code for the egghead playlist "React Hooks and Suspense"
JavaScript
128
star
57

modern-javascript

Get up to speed on the latest, most useful JavaScript features to level up your programming
JavaScript
123
star
58

kcd-discord-bot-v1

The bot for the KCD discord community
TypeScript
123
star
59

app-dev-tools

An example of how to create and hook up App DevTools to improve your development productivity of your application
JavaScript
122
star
60

preval.macro

Pre-evaluate code at build-time with babel-macros
JavaScript
120
star
61

split-guide

A tool to help generate code for workshop repositories
JavaScript
108
star
62

kcd-learning-clubs-ideas

📍 Ideas for curriculum and schedule templates for KCD Learning Clubs
106
star
63

simply-react

JavaScript
104
star
64

nps-utils

Utilities for http://npm.im/nps (npm-package-scripts)
JavaScript
100
star
65

glamorous-website

This is still a work in progress
JavaScript
98
star
66

jest-glamor-react

Jest utilities for Glamor and React
JavaScript
97
star
67

react-hooks-pitfalls

The slides and code examples for my talk "React Hook Pitfalls"
JavaScript
94
star
68

webpack-validator-DEPRECATED

Use this to save yourself some time when working on a webpack configuration.
JavaScript
93
star
69

onewheel-blog

TypeScript
90
star
70

remix-tutorial-walkthrough

I live streamed working through the Remix Jokes App Tutorial
TypeScript
87
star
71

rebase-and-merge

Making this a reality ☞
JavaScript
82
star
72

managing-state-management-slides

79
star
73

css-in-js-precompiler

WORK IN PROGRESS: Precompiles CSS-in-JS objects to CSS strings
JavaScript
72
star
74

create-react-app-react-testing-library-example

JavaScript
67
star
75

10-practical-js-features

JavaScript
67
star
76

rename-gh-to-main

JavaScript
67
star
77

full-stack-components

TypeScript
66
star
78

fakebooks-remix

The Remix version of the fakebooks app demonstrated on https://remix.run. Check out the CRA version: https://github.com/kentcdodds/fakebooks-cra
TypeScript
66
star
79

cypress-testing-workshop

A workshop for learning how to write cypress tests
JavaScript
65
star
80

prettier-eslint-atom

DEPRECATED IN FAVOR OF prettier-atom + ESLint integration
JavaScript
64
star
81

repeat-todo

A simple app I made for my wife
JavaScript
63
star
82

why-react-hooks

Talk about React hooks
JavaScript
62
star
83

codegen.macro

JavaScript
61
star
84

talks

A repo with links to talks that I've given
59
star
85

quick-stack

TypeScript
57
star
86

binode

JavaScript
57
star
87

airtable-netlify-short-urls

There's a simpler version using Netlify redirects instead of Airtable here
JavaScript
57
star
88

fully-typed-web-apps-demo

TypeScript
53
star
89

argv-set-env

Set environment variables in npm scripts
JavaScript
52
star
90

react-test-context-provider

A function that allows you to specify context to pass to a child component (intended for testing only).
JavaScript
48
star
91

concurrent-react

React Suspense Egghead course
JavaScript
47
star
92

incremental-react-router-to-remix-upgrade-path

JavaScript
46
star
93

remix-mdx

JavaScript
45
star
94

setup-prettier

JavaScript
44
star
95

podcastify-dir

Take a directory of audio files and syndicate them with an rss feed
JavaScript
42
star
96

aha-programming-slides

JavaScript
42
star
97

workshop-setup

Verify and setup a repository for workshop attendees
JavaScript
42
star
98

jest-esmodules

JavaScript
40
star
99

typing-for-kids

A little app I made for my kids for Christmas :)
JavaScript
40
star
100

react-suspense-simple-example

JavaScript
39
star