Skip to content
grucloudPublic

About

Reactive library and components for building web user interface

Resources

Stars

142 stars

Watchers

5 watching

Forks

Repository files navigation

Bau.js

A small JavaScript library to build reactive user interfaces for the web: about 3 kB minified and gzipped.

No JSX, no templates, no virtual DOM, no compiler. A component is a plain function that returns a real DOM element.

Bau is reactive: the views derive from the state. A state is a JavaScript proxy; when its value changes, the parts of the DOM that read it are updated, once the code that changed it returns, before the next paint.

import Bau from "@grucloud/bau";

const bau = Bau();
const { button, span } = bau.tags;

const counter = bau.state(0);

const Counter = () =>
  span(
    "❤️ ",
    counter,
    " ",
    button({ onclick: () => ++counter.val }, "👍"),
    button({ onclick: () => --counter.val }, "👎")
  );

// The html page has an element with the id "app"
document.getElementById("app").replaceChildren(Counter());

Try the hello world example on CodeSandbox.

Install

npm install @grucloud/bau

API

The API is a handful of functions on the object returned by Bau():

Function Description
bau.tags The HTML tags as functions: const { div, p } = bau.tags, then div({ class: "card" }, p("Hello"))
bau.tagsNS(namespace) The tags of another namespace, such as SVG or MathML
bau.state(value) A reactive state, read and written with .val. Holds a primitive, an object or an array
bau.derive(() => value) A state computed from other states, also used for side effects
bau.loop(arrayState, container, renderItem) Renders an array state into a container, applying push, splice... to the DOM without redrawing the whole list
bau.bind({ deps, render }) Renders an element from explicit state dependencies

A function given as a child or as a property is reactive: () => (show.val ? "ON" : "OFF"). Bau tracks the states it reads and runs it again when one of them changes.

The properties bauCreated, bauMounted, bauUnmounted and bauChildMutated are lifecycle methods.

TypeScript definitions are included.

Guide

The guide is written as a skill for AI coding agents, readable as plain Markdown too:

Ecosystem

Package Description
bau The core library
bau-css CSS in JS in about 35 lines of code: css, keyframes and createGlobalStyles
bau-ui 50+ themable components: Button, Input, Tabs, Tree View, Modal, Lazy...
bau-router A router: nested routes as regular expressions, link interception, under 1 kB minified and gzipped
bausaurus A static site generator for documentation written in Markdown
bau-astro Bau in Astro, for server side rendering
bau-kit A starter kit for a multi page app

Bundle size

Bundle sizes compared with bundlephobia:

bundle size

Examples

The examples directory has about 50 apps, from a counter to complete ones:

Development

The repository is a pnpm workspace:

pnpm install
pnpm test                # the unit tests of the packages
pnpm test:e2e:install    # once: the Chromium of Playwright
pnpm test:e2e            # the end to end tests of the examples
pnpm test:all            # both

To work on one example, run its own scripts: cd examples/feed-reader && npm run dev, or npm run test:e2e.

Releasing

From an up to date main, bump the minor version of all the packages, for instance from 0.202.0 to 0.203.0:

pnpm release:minor

It commits the new version, tags it, and pushes the commit and the tag.

The Publish workflow then builds, tests and publishes to npm the packages whose version is not on npm yet, with npm trusted publishing: no npm token or one-time password.

Benchmark

Bau.js 0.301.0 compared to vanilla JS and the most used frameworks, on the operations of js-framework-benchmark: create, update, swap, select and remove the rows of a table, keyed.

See the full report: every test, the memory and start-up details, tooltips with the measured values, and any framework as the reference.

Each framework compared to Bau.js:

CPU memory start-up size (gzip) code
Vanilla JS 7% faster 22% less 38% faster 3.7× smaller 11% longer
SolidJS 2.0.4 4% faster 18% more 29% slower 4.7× larger 8% shorter
Svelte 5.60.0 1% faster 30% more 44% slower 3.2× larger 10% shorter
Vue 3.5.46 2% slower 52% more 51% slower 5.0× larger 6% longer
Angular 22.3.2 41% slower 43% more 2.1× slower 7.3× larger 13% longer
React 19.3.1 43% slower 75% more 3.2× slower 13.3× larger 7% longer

Vanilla JS, hand-written DOM manipulation, is the baseline: the fastest possible. Among the frameworks:

  • Memory: Bau.js uses the least: SolidJS, the next one, uses 18% more, React 75% more.
  • Start-up: Bau.js starts the fastest: SolidJS, the next one, starts 29% slower, React 3.2 times slower.
  • CPU: Bau.js is almost as fast as SolidJS and Svelte, within 4%, without a compiler: they compile their components, Bau.js runs plain JavaScript functions.
  • Size: Bau.js is the smallest, 3.2 times smaller than Svelte, the next one.
  • Code: the Bau.js implementation is shorter than the vanilla JS, Vue, Angular and React ones; Svelte and SolidJS are slightly shorter, 10% and 8%, with the syntax of their compilers.
  • React, Vue and Angular: Bau.js beats them on every measure: CPU (Vue within 2%), memory, start-up, size and code.

CPU, memory and start-up: the geometric mean over their tests. Size: the JavaScript of the page. Code: the characters of each implementation. Measured on an Apple M2 with Chromium 159, 3 iterations, on 3 December 2026; the benchmark is in examples/benchmark.

Chrome Lighthouse gives a performance score of 100 to an app built with all the Bau UI components.

bau lighthouse

History

Bau is inspired by VanJS. The differences:

  • A Bau state holds primitives, objects and arrays. Mutating an array, like todos.val.push(todo), updates the DOM without rendering the whole list again.

  • Lifecycle methods: bauCreated, bauMounted, bauUnmounted and bauChildMutated.

  • No global: each call of Bau() creates an instance, which can also be given a window.

    import Bau from "@grucloud/bau";
    const bau = Bau();
    const { div } = bau.tags;
  • A reactive function can return an array of elements, like a React Fragment.

  • One paradigm: the views derive from the state. The DOM is not changed by hand, which makes Bau hard to misuse.

  • An attribute can be undefined or null, see issue 39.

Contributing

Please report bugs and suggestions on GitHub.

License

MIT

About

Reactive library and components for building web user interface

Resources

Stars

142 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages