npm.io
1.4.0 • Published 2d ago

@tuananh/sax-parser

Licence
MIT
Version
1.4.0
Deps
3
Size
185 kB
Vulns
0
Weekly
0
Stars
8
Install scriptsThis package runs scripts during installation (preinstall/install/postinstall)

sax-parser npm version github actions ci license

need a logo, this is just sth i found on the web

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-parser

Benchmark

benchmark/index.js compares SAX-style parsers on a ~10 KB XML document. Each parser registers noop startElement, endElement, and text handlers so the comparison includes event dispatch overhead.

npm run benchmark

Results on Node.js v26.5.0, Linux x64:

module ops/sec native XML compliant stream
@tuananh/sax-parser 16,962
easysax 11,126
saxophone 7,543
ltx 3,853
sax 1,551
node-expat 1,200
node-xml 759

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 in this comparison while remaining a native parser with streaming and full XML compliance.

Usage

  • See example/print.js for an example how to use this library to pretty print XML to process.stdout.
  • Or example/stream.js for 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 git@github.com:tuananh/sax-parser.git
cd sax-parser
npm install
npm run build
node example/print.js
npm run test

Credits

Keywords