Perfect JavaScript Example
Technical portfolio with no frameworks, no libraries, no build step: just modern JavaScript (ES2023+), semantic HTML and lean CSS. Every section below is a real demo — and shows the source code that implements it.
Modules loaded:
criarContador() with no global variable. I used closures because they
encapsulate state without classes; compose/pipe/curry compose pure logic; memoize caches
results and the comparison uses performance.now().
async/await makes asynchronous code sequential and readable;
Promise.all parallelizes, allSettled tolerates partial failures
and race + timeout implement a deadline. I used
AbortController because cancelling requests is mandatory in real UIs, and
retry with backoff because networks fail — loading/error/empty/success states
always show up.
Promise.then, queueMicrotask) drains completely, and only then
come macrotasks (setTimeout) and requestAnimationFrame. I
demonstrate this by running the sequence and numbering each step — handy for understanding
concurrency bugs.
<template> and
DocumentFragment to render large lists in a single reflow — instead of
N.
structuredClone) for traceable changes, selectors for targeted reads,
Proxy for reactivity and localStorage for persistence — with
try/catch, because storage can be blocked.
# give real encapsulation (not a convention, syntax);
getters/setters validate; static blocks configure the class at load time;
inheritance and mixins cover reuse, and WeakMap/Symbol are the
"old-school" alternatives compared side by side with a factory function.
function* produces values on demand — perfect for infinite sequences and
pagination; Symbol.iterator makes any object iterable by for…of;
async generators + for await...of consume paginated APIs elegantly. I
included Map/Set/WeakSet and iterator-helper detection with a fallback.
Worker runs the calculation on another thread with postMessage,
a progress bar and cancellation via worker.terminate(). The demo compares
frames rendered with and without the worker — the difference is visible.
setCustomValidity,
checkValidity, reportValidity) gives native validation with
custom messages; hand-made CPF/CEP/phone masks, real-time validation with
debounce, messages wired via
aria-describedby/aria-live, and FormData +
Object.fromEntries for clean collection.
localStorage, and every move is announced in
aria-live.
Intl API covers currency, dates, relative time, lists, plurals
and collation — zero libraries. Switching between pt-BR/en-US/es updates the DOM
instantly, without reloading, using JSON dictionaries and textContent.
requestAnimationFrame syncs with the frame; virtualization renders only the
visible window of 10,000 items (~20 DOM nodes, not 10,000);
performance.mark/measure measures with millisecond precision.
<dialog> — all with feature detection ('x' in window) and
a friendly fallback. No permission is requested automatically: geolocation and
notifications are opt-in.
Tests
The utilities have unit tests with Node's native runner (node:test +
node:assert) — no Jest, no Vitest. Run with npm test.
-
tests/debounce.test.js— timers withmock.timers -
tests/throttle.test.js— leading/trailing/cancel -
tests/memoize.test.js— cache, resolver, clear -
tests/compose.test.js— compose/pipe/curry and errors -
tests/retry.test.js— exponential backoff, cause, abort -
tests/store.test.js— subscribe/unsubscribe/immutability -
tests/validators.test.js— CPF/CEP/e-mail/password -
tests/format.test.js— Intl number/date/list/plural/collate -
tests/highlight.test.js— pure tokenizer for<code-peek>
How it was built
Every module has a standard header (ARQUIVO / PROPÓSITO / CONCEITOS / USADO EM) and complete JSDoc on its exports. Start with the pure utilities — they are the testable foundation of everything.
-
core/dom.js
—
$,h(), delegation helpers - core/store.js — immutable pub/sub mini store
- core/event-bus.js — EventTarget as a message bus
- core/router.js — hash router + History API
- core/storage.js — safe localStorage with fallback
- core/logger.js — leveled logger toggled by flag
- utils/debounce.js — delay on inactivity
- utils/throttle.js — rate limit per interval
- utils/memoize.js — cache by arguments
- utils/compose.js — compose/pipe/curry
- utils/retry.js — retry with exponential backoff
- utils/sleep.js — cancellable sleep
- utils/deep-clone.js — deep clone with fallback
- utils/format.js — Intl formatting
- utils/validators.js — pure validators
- utils/highlight.js — syntax tokenizer
- components/code-peek.js — renders the real source code
- components/demo-card.js — section card
- components/toast-notification.js — notifications
- components/theme-toggle.js — light/dark/system theme
- components/accessible-tabs.js — accessible ARIA tabs
- core/i18n.js — language dictionaries (en-US/pt-BR)
- docs/ARCHITECTURE.md — diagram and demo lifecycle
- docs/JS-TECHNIQUES.md — techniques table