
PFMCODESI built a code editor library because Monaco frustrated me Monaco was slow, buggy, and the...
Monaco was slow, buggy, and the syntax highlighting wasn't even that good. So I built my own.
Caret is a lightweight code editor library built on the EditContext API. 605 lines of code. ~32KB. Loads in ~25ms.
Monaco was my go-to for other projects. But after dealing with slow load times, sync bugs, and unreliable highlighting, I decided to start from scratch. The first version shipped in a few days. The current version is a complete rewrite — cleaner, faster, and a fraction of the size.
Here's how it compares:
| Caret | Monaco | CodeMirror 6 | |
|---|---|---|---|
| Bundle size | ~32KB | ~5MB | ~400KB |
| Load time | ~25ms | ~2-3s | ~500ms |
| Lines of code | 650 | ~300,000 | ~50,000 |
| Architecture | EditContext | textarea | contenteditable |
Previous versions used a textarea-based input model, which caused constant caret synchronization issues. The current version (v0.3+) is built entirely on the EditContext API — a browser API designed specifically for custom editors. It gives direct control over text input with no hacks.
How it works under the hood:
text is the source of truth — no dual-layer sync issuesrender() calls hljs.highlight() and updates the DOMRange.getBoundingClientRect() — no canvas mathThe codebase is split into five focused modules:
textEditor.js — core editor logic (400 lines)caret.js — pixel-perfect caret positioning via Range APIlineCounter.js — line number trackingfont.js — custom font loadinglanguages.js — Highlight.js language registrationThemes supported: Tokyo Night, Monokai, GitHub Dark, Atom One Dark, Nord, Night Owl, VS2015, and more.
Languages supported: JavaScript, Python, HTML, and anything Highlight.js covers.
npm install @pfmcodes/caret
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="editor"></div>
<script type="module">
import Caret from 'https://esm.sh/@pfmcodes/caret';
const editor = await Caret.createEditor(
document.getElementById('editor'),
'// Start coding...',
{
id: 'my-editor',
dark: true,
language: 'javascript',
hlTheme: 'tokyo-night-dark'
}
);
</script>
</body>
</html>
Caret v0.3+ uses the EditContext API, which is currently only available in Chromium-based browsers (Chrome, Edge). Firefox and Safari are not supported yet — Firefox tracking issue here.