speedy-entities - v3.0.0

speedy-entities 🏎💨 build

The fastest XML/HTML entity encoder/decoder in 23 kB gzipped.

npm install --save-prod speedy-entities

Decode

There are two preconfigured decoders: decodeXML and decodeHTML.

import { decodeXML, decodeHTML } from 'speedy-entities';

decodeXML('ab&lt');
// ⮕ 'ab&lt'

decodeHTML('&ltfoo&AElig');
// ⮕ '<foo\u00c6'

decodeHTML('&NotNestedGreaterGreater;&CounterClockwiseContourIntegral;');
// ⮕ '\u2aa2\u0338\u2233'

Custom decoder

You can create a decoder that decodes custom named entities:

import { arrayTrieEncode, trieCreate, trieSet } from '@smikhalevski/trie';
import { createEntityDecoder } from 'speedy-entities';

// Create a trie that would hold entities
const trie = trieCreate<string>();

// Register named entities
trieSet('foo;', 'okay');
trieSet('qux;', 'yeah');

// Encode a trie
const entitiesTrie = arrayTrieEncode(trie);

// Create a decoder
const decode = createEntityDecoder({
entitiesTrie,
numericReferenceSemicolonRequired: true,
});

// Decode entities
decode('&foo;');
// ⮕ 'okay'

decode('&foo');
// ⮕ '&foo'

// Decode numeric character references
decode('&#X61;&#x62;&#x63;');
// ⮕ '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('&😘❤️');
// ⮕ '&amp;&#x1f618;&#x2764;&#xfe0f;'

escapeXML('&😘❤️');
// ⮕ '&amp;😘❤'

Performance

Clone this repo and use npm ci && npm run perf to run the performance testsuite.

Results are in millions of operations per second. The higher number is better.

Decode

Decode HTML performance chart

Encode

Encode XML performance chart

Generated using TypeDoc