speedy-entities
v3.1.0
Published
The fastest XML/HTML entities decoder.
Maintainers
Readme
speedy-entities 🏎💨
The fastest XML/HTML entity encoder/decoder in 13 kB gzipped.
npm install --save-prod speedy-entitiesDecode
There are two preconfigured decoders: decodeXML and decodeHTML.
import { decodeXML, decodeHTML } from 'speedy-entities';
decodeXML('ab<');
// ⮕ 'ab<'
decodeHTML('<fooÆ');
// ⮕ '<foo\u00c6'
decodeHTML('⪢̸∳');
// ⮕ '\u2aa2\u0338\u2233'Custom decoder
You can create a decoder that decodes your custom character entity references:
import { createEntityDecoder } from 'speedy-entities';
// Create a decoder
const decode = createEntityDecoder({
entities: {
'&foo;': 'okay',
'&qux;': 'yeah',
},
isNumericReferenceSemicolonRequired: true,
});
// Decode entities
decode('&foo;');
// ⮕ 'okay'
decode('&foo');
// ⮕ '&foo'
// Decode numeric character references
decode('abc');
// ⮕ 'abc'Encode
encodeXML encodes non-ASCII characters as named XML entities or as numeric references.
escapeXML escapes only "&'<> characters.
import { encodeXML, escapeXML } from 'speedy-entities';
encodeXML('&😘❤️');
// ⮕ '&😘❤️'
escapeXML('&😘❤️');
// ⮕ '&😘❤'Performance
Results are in millions of operations per second (MHz). The higher number is better.
| Library | Decode HTML | Decode XML | Encode XML | Escape XML | | --------------------------------: | ----------: | ---------: | ---------: | ---------: | | speedy-entities@3.1.0 | 4.9 | 5.2 | 5.3 | 7.2 | | entities@6.0.1 | 3.4 | 3.6 | 4.5 | 6.1 | | html-entities@2.6.0 | 2.2 | 2.3 | 3.3 | 5.1 | | he@1.2.0 | 1.9 | 1.8 | 2.2 | 5.2 |
Tests were conducted using TooFast on Apple M1 with Node.js v23.11.1.
To reproduce the performance test suite results, clone this repo and run:
npm ci
npm run build
npm run perf