@tuananh/sax-parser
v1.7.0
Published
An extremely fast SAX parser for Node.js, written in C++.
Maintainers
Readme
sax-parser

What this is
A very fast SAX parser for Node.js written in C++. Native module for performance reason.
Installation
pnpm install @tuananh/sax-parser
# npm install @tuananh/sax-parserBenchmark
benchmark/index.mjs and benchmark/streaming.mjs compare SAX-style parsers on a ~10 KB XML document. Each parser registers noop handlers that accept the usual event arguments so the comparison includes string decoding and dispatch overhead.
npm run benchmark
npm run benchmark:streamingResults on Node.js v26.5.0, Linux x64:
Single-write (npm run benchmark)
| module | ops/sec | native | XML compliant | stream | | ------------------- | ------- | ------ | ------------- | ------ | | @tuananh/sax-parser | 20,821 | ✅ | ✅ | ✅ | | @eksml/xml | 15,495 | ❌ | ✅ | ✅ | | saxophone | 5,951 | ❌ | ✅ | ✅ | | easysax | 5,131 | ❌ | ✅ | ✅ | | ltx | 4,116 | ❌ | ❌ | ✅ | | sax | 1,522 | ❌ | ✅ | ✅ | | node-expat | 1,224 | ✅ | ✅ | ✅ | | node-xml | 735 | ❌ | ✅ | ✅ |
Streaming (npm run benchmark:streaming)
Same document delivered in small chunks (fresh parser per iteration):
| module | 256 B chunks | 64 B chunks | | ------------------- | ------------ | ----------- | | @tuananh/sax-parser | 8,484 | 8,560 | | @eksml/xml | 9,237 | 8,045 | | saxophone | 4,784 | 3,970 | | easysax | 4,491 | 4,172 | | ltx | 3,813 | 3,222 | | sax | 1,597 | 1,579 | | node-expat | 1,191 | 1,076 | | node-xml | 679 | 587 |
ops/sec: higher is better.
ltx is included for reference — it is fast but not fully XML spec compliant. @tuananh/sax-parser is the fastest single-write parser in this comparison while remaining a native parser with streaming and full XML compliance. On chunked input it leads at 64 B chunks and stays close to @eksml/xml at 256 B.
Usage
- See
example/print.jsfor an example how to use this library to pretty print XML toprocess.stdout. - Or
example/stream.jsfor an example of using this library with stream. - For complete API documentation, see API.md.
Sample usage
const { Readable } = require('stream')
const SaxParser = require('@tuananh/sax-parser')
const parser = new SaxParser()
const xml = '<hello><item id="1"><name>foo</name></item></hello>'
const readStream = new Readable()
readStream._read = () => {}
readStream.push(xml)
readStream.push(null)
readStream
.pipe(parser)
.on('startElement', (name, attrs) => {
console.log('name', name)
})
.on('text', (text) => {
console.log('text', text)
})
.on('end', () => {
console.log('done')
})Development
You will need to have all node-gyp's requirements installed.
git clone [email protected]:tuananh/sax-parser.git
cd sax-parser
npm install
npm run build
node example/print.js
npm run testCredits
- engine-x: a fork of cocos2d-x game engine
- xsxml: The embedded xml SAX parser, extract from pugixml/rapidxml DOM parsers
- addon-event-emitter: How to create and use event emitter interface on Node.js add-ons
- addon-stream: How to use and create stream on Node.js native add-ons
- napi-example-transformstream: Simple transform stream implementation using node-addon-api
- Node.js C++ addon examples: Official Node.js C++ addon examples from Node.js
