Minimal RDF library optimized for JSON/Linked Objects only
Code golf version of rdflib.js with 97% smaller bundle size and zero dependencies.
| Library | Minified | Dependencies | Source Lines | Savings |
|---|---|---|---|---|
| rdflib.js | 264 KB | 4.9 MB | 14,374 | - |
| rdflib-lite | ~8 KB | 0 KB | ~500 | 97% |
✅ JSON-LD / LION Parsing - Full support for linked objects notation ✅ JSON Serialization - Convert RDF back to JSON-LD ✅ RDF Store - In-memory triple store with indexing ✅ RDF Terms - NamedNode, BlankNode, Literal, Quad ✅ Query - Basic pattern matching ✅ Zero Dependencies - No external libraries
All the bloat that you don't need for JSON/LION:
- ❌ Turtle / N3 Parser (1,610 lines) - saves 684 KB n3 dependency
- ❌ RDF/XML Parser (457 lines) - saves 216 KB xmldom dependency
- ❌ RDFa Parser (954 lines) - saves 216 KB xmldom dependency
- ❌ SPARQL Query Engine (1,118 lines)
- ❌ Update Manager (1,272 lines)
- ❌ HTTP Fetcher (2,205 lines) - saves 332 KB cross-fetch dependency
- ❌ jsonld library (2.2 MB!) - reimplemented minimal version
Total removed: ~8,200 lines of code and ~4.9 MB of dependencies
npm install rdflib-liteOr just copy rdflib-lite.js - it's a single file with zero dependencies!
import { Store, parseJSON, serializeJSON } from 'rdflib-lite'
// Create a store
const store = new Store()
// Parse JSON-LD / LION
const data = {
'@id': 'http://example.org/person/1',
'@type': 'Person',
name: 'John Lennon',
born: '1940-10-09'
}
parseJSON(data, store)
// Query
const person = store.sym('http://example.org/person/1')
const statements = store.match(person, null, null)
console.log(`Found ${statements.length} statements`)
// Serialize back to JSON
const json = serializeJSON(store, { pretty: true })
console.log(json)import { Store, parseJSON, serializeJSON } from 'rdflib-lite'
import { create, fetch } from 'linkedobjects'
// Linked objects container
const objects = {}
// Create some linked objects
create(objects, 'http://example.org/album/1', {
name: 'Abbey Road',
releaseDate: '1969-09-26'
})
// Parse into RDF store
const store = new Store()
parseJSON(objects['http://example.org/album/1'], store)
// Now you have RDF triples!
console.log(store.statements)const store = new Store()Methods:
sym(uri)- Create NamedNodeliteral(value, lang, datatype)- Create Literalbnode(id)- Create BlankNodequad(s, p, o, g)- Create Quadadd(s, p, o, g)- Add statementmatch(s, p, o, g)- Query statementsremove(s, p, o, g)- Remove statementssubjects()- Get all subjectsclear()- Clear store
Parse JSON-LD or LION into RDF store.
Parameters:
data- JSON object or stringstore- RDF Store instancebaseURI- Base URI for relative URIs (optional)
Returns: Store
Serialize RDF store to JSON-LD.
Parameters:
store- RDF Store instanceoptions- { pretty: boolean, context: object }
Returns: JSON string
Use the compatibility adapter to work with existing rdflib code:
import { LightweightRDFStore, linkedObjectsToRDF } from './rdflib-adapter.js'
import * as linkedobjects from 'linkedobjects'
// Create linked objects
const objects = {}
linkedobjects.create(objects, 'http://example.org/1', { name: 'Test' })
// Convert to RDF
const store = new LightweightRDFStore()
linkedObjectsToRDF(objects, store)
// Now compatible with rdflib-expecting code
console.log(store.toNTriples())Use rdflib-lite when:
- ✅ You only need JSON-LD / LION support
- ✅ Bundle size matters (mobile, edge computing)
- ✅ You want zero dependencies
- ✅ Simple RDF operations are sufficient
- ✅ Working with linkedobjects library
Use full rdflib.js when:
- ❌ You need Turtle, N3, RDF/XML, or RDFa parsing
- ❌ You need SPARQL queries
- ❌ You need remote fetching with content negotiation
- ❌ You need update/patch management
- ❌ Complex graph operations required
Single file: rdflib-lite.js
rdflib-lite.js (~500 lines, ~8 KB minified)
├── RDF Terms (NamedNode, BlankNode, Literal, Quad)
├── RDF Store (indexed triple store)
├── JSON-LD Parser (minimal, no external deps)
├── JSON Serializer
└── Query (basic pattern matching)
Compare to rdflib.js:
rdflib.js (14,374 lines, 264 KB minified, 4.9 MB deps)
├── RDF Terms ✓
├── RDF Store ✓
├── JSON-LD Parser (requires jsonld@2.2MB) ✓
├── N3/Turtle Parser (requires n3@684KB) ✗
├── RDF/XML Parser (requires xmldom@216KB) ✗
├── RDFa Parser (requires xmldom@216KB) ✗
├── SPARQL Query Engine ✗
├── Update Manager ✗
├── HTTP Fetcher (requires cross-fetch@332KB) ✗
└── Complex graph operations ✗
Starting point: rdflib.js @ 264 KB minified, 4.9 MB total
Optimizations applied:
- ✂️ Removed N3/Turtle parser → saved 1,610 lines, 684 KB
- ✂️ Removed RDF/XML parser → saved 457 lines, 216 KB
- ✂️ Removed RDFa parser → saved 954 lines, 216 KB
- ✂️ Removed SPARQL engine → saved 1,118 lines
- ✂️ Removed Update Manager → saved 1,272 lines
- ✂️ Removed HTTP Fetcher → saved 2,205 lines, 332 KB
- ✂️ Removed jsonld dependency → saved 2.2 MB (reimplemented minimal)
- ✂️ Removed Babel runtime → saved 1.1 MB
Final result: ~8 KB minified, 0 dependencies
Total savings:
- Source code: 96.5% reduction (14,374 → 500 lines)
- Minified bundle: 97% reduction (264 KB → 8 KB)
- Dependencies: 100% reduction (4.9 MB → 0 KB)
Run the test page to see live comparisons:
python3 -m http.server 8000Then open: http://localhost:8000/rdflib-test.html
MIT (same as rdflib.js)
Based on rdflib.js by the Solid project. Optimized for linkedobjects - a simpler approach to Linked Data.
rdflib-lite: Because sometimes less is more. 97% more efficient. 🚀