Project: syntax-tree/unist-util-size

Package: unist-util-size@3.0.0

  1. Dependents: 0
  2. unist utility to calculate the number of nodes in a tree
  1. util 147
  2. utility 143
  3. unist 131
  4. unist-util 25
  5. node 25
  6. count 4
  7. size 2


Build Coverage Downloads Size Sponsors Backers Chat

unist utility to calculate the number of nodes in a tree.


What is this?

This is a tiny utility that you can use to count nodes (that pass a test) in a tree.

When should I use this?

This is super tiny but a sometimes common problem. You can use unist-util-visit yourself as well.


This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with npm:

npm install unist-util-size

In Deno with esm.sh:

import {size} from "https://esm.sh/unist-util-size@3"

In browsers with esm.sh:

<script type="module">
  import {size} from "https://esm.sh/unist-util-size@3?bundle"


import {h} from 'hastscript'
import {size} from 'unist-util-size'

const tree = h('div', [
  h('p', [
    'Some ',
    h('strong', 'importance'),
    ' and ',
    h('em', 'emphasis'),
  h('pre', h('code', 'bar()'))

console.log(size(tree)) // => 11
console.log(size(tree, 'element')) // => 5


This package exports the identifier size. There is no default export.

size(node[, test])

Calculate the number of nodes in node (Node) that pass the optional unist-util-is-compatible Test.


Exclusive descendants of node that pass test (number).


This package is fully typed with TypeScript. It exports no additional types.


Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. Our projects sometimes work with older versions, but this is not guaranteed.


See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.


MIT © Titus Wormer