Skip to content

Commit 9b6832b

Browse files
committed
Used box to control availability of memory for dispatched events
1 parent 2aa87ce commit 9b6832b

19 files changed

+87
-64
lines changed

README.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ All parsers are tested using a large XML document (3 MB) containing a variety of
2020

2121
| Parser with Advanced Features | time/ms (lower is better)| JS | Runs in browser |
2222
|--------------------------------------------------------------------------------------------|-------------------------:|:------:|:---------------:|
23-
| [sax-wasm](https://github.com/justinwilaby/sax-wasm) | 26.67 |||
24-
| [saxes](https://github.com/lddubeau/saxes) | 40.99 |||
25-
| [ltx(using Saxes as the parser)](https://github.com/xmppjs/ltx) | 41.87 |||
26-
| [sax-js](https://github.com/isaacs/sax-js) | 105.18 ||* |
27-
| [node-xml](https://github.com/dylang/node-xml) | 115.17 |||
28-
| [node-expat](https://github.com/xmppo/node-expat) | 148.885 |||
23+
| [sax-wasm](https://github.com/justinwilaby/sax-wasm) | 18.54 |||
24+
| [saxes](https://github.com/lddubeau/saxes) | 41.01 |||
25+
| [ltx(using Saxes as the parser)](https://github.com/xmppjs/ltx) | 44.56 |||
26+
| [sax-js](https://github.com/isaacs/sax-js) | 116.98 ||* |
27+
| [node-xml](https://github.com/dylang/node-xml) | 124.49 |||
28+
| [node-expat](https://github.com/xmppo/node-expat) | 149.61 |||
2929
<sub>*built for node but *should* run in the browser</sub>
3030

3131
## Installation
@@ -88,17 +88,22 @@ if (ready) {
8888
}
8989
}
9090
```
91+
## The 'Lifetime' of events
92+
`Tag`, `Attribute`, `ProcInst` and `Text` objects received from the parsing operation have a 'lifetime' that is limited to the `eventHandler()` or the function loop body for the `*parse()` generator. Data sent across the FFI (Foreign Function Interface) boundary is read directly from WASM memory which is partly why sax-wasm is so fast. This comes with the tradeoff that this memory is temporary because it is overwritten on the next write operation. If you need to persist the event data for long term use, call `toJSON()` on each object as needed. This comes at a slight performance cost and should not be necessary for the vast majority of use cases.
9193
92-
## Differences from sax-js
93-
Besides being incredibly fast, there are some notable differences between sax-wasm and sax-js that may affect some users
94-
when migrating:
94+
## Differences from other parsers
95+
Besides being incredibly fast, there are some notable differences between other SAX style parsers:
9596
97+
1. This repo is maintained
98+
1. UTF-16 encoded documents are supported. 1-4 byte graphemes are fully supported even if streaming causes a break between surrogates.
9699
1. JSX is supported including JSX fragments. Things like `<foo bar={this.bar()}></bar>` and `<><foo/><bar/></>` will parse as expected.
97100
1. Angular 2+ templates are supported. Things like <button type="submit" [disabled]=disabled *ngIf=boolean (click)="clickHandler(event)"></button> will parse as expected.
101+
1. HTML is supported provided it is not a "quirks mode" document that ran in IE9.
98102
1. No attempt is made to validate the document. sax-wasm reports what it sees. If you need strict mode or document validation, it may
99103
be recreated by applying rules to the events that are reported by the parser.
100104
1. Namespaces are reported in attributes. No special events dedicated to namespaces.
101105
1. Streaming utf-8 code points in a Uint8Array is required.
106+
1. Whitespace between XML elements is not reported. If you need this, a simple subtraction of the `line` and `character` between the end of one tag and the start of the next will reveal where this whitespace exists.
102107
103108
## Streaming
104109
Streaming is supported with sax-wasm by writing utf-8 code points (Uint8Array) to the parser instance. Writes can occur safely

lib/cjs/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ const SaxWasm = require("./saxWasm.js");
2020
__exportStar(require("./saxWasm.js"), exports);
2121
// Export everything as the default export
2222
exports.default = SaxWasm;
23-
//# sourceMappingURL=index.js.map

lib/cjs/index.js.map

-1
This file was deleted.

lib/cjs/saxWasm.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export declare class SAXParser {
420420
* })();
421421
* ```
422422
*/
423-
parse(reader: ReadableStreamDefaultReader<Uint8Array>): AsyncGenerator<[SaxEventType, Detail]>;
423+
parse(reader: ReadableStreamDefaultReader<Uint8Array>): AsyncGenerator<[SaxEventType, Reader<Detail>]>;
424424
/**
425425
* Writes a chunk of data to the parser.
426426
*

lib/cjs/saxWasm.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ class SAXParser {
485485
async *parse(reader) {
486486
let eventAggregator = [];
487487
this.eventHandler = function (event, detail) {
488-
eventAggregator.push([event, detail.toJSON()]);
488+
eventAggregator.push([event, detail]);
489489
};
490490
while (true) {
491491
const chunk = await reader.read();
@@ -629,4 +629,3 @@ const readPosition = (uint8Array, ptr = 0) => {
629629
return new Position(line, character);
630630
};
631631
exports.readPosition = readPosition;
632-
//# sourceMappingURL=saxWasm.js.map

lib/cjs/saxWasm.js.map

-1
This file was deleted.

lib/esm/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ import * as SaxWasm from './saxWasm.js';
44
export * from './saxWasm.js';
55
// Export everything as the default export
66
export default SaxWasm;
7-
//# sourceMappingURL=index.js.map

lib/esm/index.js.map

-1
This file was deleted.

lib/esm/saxWasm.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export declare class SAXParser {
420420
* })();
421421
* ```
422422
*/
423-
parse(reader: ReadableStreamDefaultReader<Uint8Array>): AsyncGenerator<[SaxEventType, Detail]>;
423+
parse(reader: ReadableStreamDefaultReader<Uint8Array>): AsyncGenerator<[SaxEventType, Reader<Detail>]>;
424424
/**
425425
* Writes a chunk of data to the parser.
426426
*

lib/esm/saxWasm.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ export class SAXParser {
476476
async *parse(reader) {
477477
let eventAggregator = [];
478478
this.eventHandler = function (event, detail) {
479-
eventAggregator.push([event, detail.toJSON()]);
479+
eventAggregator.push([event, detail]);
480480
};
481481
while (true) {
482482
const chunk = await reader.read();
@@ -616,4 +616,3 @@ export const readPosition = (uint8Array, ptr = 0) => {
616616
const character = readU32(uint8Array, ptr + 4);
617617
return new Position(line, character);
618618
};
619-
//# sourceMappingURL=saxWasm.js.map

0 commit comments

Comments
 (0)