Skip to content

Commit 72c7819

Browse files
committed
fix: significantly reduce package size
1 parent ee77083 commit 72c7819

File tree

4 files changed

+159
-4429
lines changed

4 files changed

+159
-4429
lines changed

README.md

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -128,68 +128,71 @@ Common
128128
129129
Initialization / Load speed
130130
131-
* #1: html-entities x 2,632,942 ops/sec ±3.71% (72 runs sampled)
132-
#2: entities x 1,379,154 ops/sec ±5.87% (75 runs sampled)
133-
#3: he x 1,334,035 ops/sec ±3.14% (83 runs sampled)
131+
#1: he x 516 ops/sec ±5.71% (78 runs sampled)
132+
* #2: html-entities x 407 ops/sec ±5.64% (81 runs sampled)
133+
#3: entities x 352 ops/sec ±4.16% (80 runs sampled)
134134
135135
HTML5
136136
137137
Encode test
138138
139-
* #1: html-entities.encode - html5, nonAscii x 415,806 ops/sec ±0.73% (85 runs sampled)
140-
* #2: html-entities.encode - html5, nonAsciiPrintable x 401,420 ops/sec ±0.35% (93 runs sampled)
141-
#3: entities.encodeNonAsciiHTML x 401,235 ops/sec ±0.41% (88 runs sampled)
142-
#4: entities.encodeHTML x 284,868 ops/sec ±0.45% (93 runs sampled)
143-
* #5: html-entities.encode - html5, extensive x 237,613 ops/sec ±0.42% (93 runs sampled)
144-
#6: he.encode x 91,459 ops/sec ±0.50% (84 runs sampled)
139+
* #1: html-entities.encode - html5, extensive x 437,236 ops/sec ±0.90% (98 runs sampled)
140+
#2: entities.encodeHTML x 335,714 ops/sec ±0.87% (92 runs sampled)
141+
142+
Encode non-ASCII test
143+
144+
* #1: html-entities.encode - html5, nonAscii x 749,246 ops/sec ±0.61% (96 runs sampled)
145+
#2: entities.encodeNonAsciiHTML x 706,984 ops/sec ±1.06% (98 runs sampled)
146+
* #3: html-entities.encode - html5, nonAsciiPrintable x 691,193 ops/sec ±4.47% (90 runs sampled)
147+
#4: he.encode x 141,105 ops/sec ±0.87% (92 runs sampled)
145148
146149
Decode test
147150
148-
#1: entities.decodeHTMLStrict x 614,920 ops/sec ±0.41% (89 runs sampled)
149-
#2: entities.decodeHTML x 577,698 ops/sec ±0.44% (90 runs sampled)
150-
* #3: html-entities.decode - html5, strict x 323,680 ops/sec ±0.39% (92 runs sampled)
151-
* #4: html-entities.decode - html5, body x 297,548 ops/sec ±0.45% (91 runs sampled)
152-
* #5: html-entities.decode - html5, attribute x 293,617 ops/sec ±0.37% (94 runs sampled)
153-
#6: he.decode x 145,383 ops/sec ±0.36% (94 runs sampled)
151+
#1: entities.decodeHTML x 678,595 ops/sec ±1.28% (92 runs sampled)
152+
#2: entities.decodeHTMLStrict x 684,372 ops/sec ±2.76% (82 runs sampled)
153+
* #3: html-entities.decode - html5, strict x 485,664 ops/sec ±0.80% (94 runs sampled)
154+
* #4: html-entities.decode - html5, body x 463,074 ops/sec ±1.11% (93 runs sampled)
155+
* #5: html-entities.decode - html5, attribute x 456,185 ops/sec ±2.24% (91 runs sampled)
156+
#6: he.decode x 302,668 ops/sec ±2.73% (90 runs sampled)
154157
155158
HTML4
156159
157160
Encode test
158161
159-
* #1: html-entities.encode - html4, nonAscii x 379,799 ops/sec ±0.29% (96 runs sampled)
160-
* #2: html-entities.encode - html4, nonAsciiPrintable x 350,003 ops/sec ±0.42% (92 runs sampled)
161-
* #3: html-entities.encode - html4, extensive x 169,759 ops/sec ±0.43% (90 runs sampled)
162+
* #1: html-entities.encode - html4, nonAscii x 737,475 ops/sec ±1.04% (95 runs sampled)
163+
* #2: html-entities.encode - html4, nonAsciiPrintable x 649,866 ops/sec ±4.28% (79 runs sampled)
164+
* #3: html-entities.encode - html4, extensive x 202,337 ops/sec ±3.66% (64 runs sampled)
162165
163166
Decode test
164167
165-
* #1: html-entities.decode - html4, attribute x 291,048 ops/sec ±0.42% (92 runs sampled)
166-
* #2: html-entities.decode - html4, strict x 287,110 ops/sec ±0.56% (93 runs sampled)
167-
* #3: html-entities.decode - html4, body x 285,529 ops/sec ±0.57% (93 runs sampled)
168+
* #1: html-entities.decode - html4, attribute x 529,674 ops/sec ±0.90% (90 runs sampled)
169+
* #2: html-entities.decode - html4, body x 499,135 ops/sec ±2.27% (80 runs sampled)
170+
* #3: html-entities.decode - html4, strict x 489,806 ops/sec ±4.37% (84 runs sampled)
168171
169172
XML
170173
171174
Encode test
172175
173-
#1: entities.encodeXML x 418,561 ops/sec ±0.80% (90 runs sampled)
174-
* #2: html-entities.encode - xml, nonAsciiPrintable x 402,868 ops/sec ±0.30% (89 runs sampled)
175-
* #3: html-entities.encode - xml, nonAscii x 403,669 ops/sec ±7.87% (83 runs sampled)
176-
* #4: html-entities.encode - xml, extensive x 237,766 ops/sec ±0.45% (93 runs sampled)
176+
* #1: html-entities.encode - xml, nonAscii x 823,097 ops/sec ±0.75% (81 runs sampled)
177+
* #2: html-entities.encode - xml, nonAsciiPrintable x 764,638 ops/sec ±0.93% (93 runs sampled)
178+
#3: entities.encodeXML x 672,186 ops/sec ±1.51% (92 runs sampled)
179+
* #4: html-entities.encode - xml, extensive x 376,870 ops/sec ±0.76% (77 runs sampled)
177180
178181
Decode test
179182
180-
#1: entities.decodeXML x 888,700 ops/sec ±0.48% (93 runs sampled)
181-
* #2: html-entities.decode - xml, strict x 353,127 ops/sec ±0.40% (92 runs sampled)
182-
* #3: html-entities.decode - xml, body x 355,796 ops/sec ±1.58% (86 runs sampled)
183-
* #4: html-entities.decode - xml, attribute x 369,454 ops/sec ±8.74% (84 runs sampled)
183+
#1: entities.decodeXML x 930,758 ops/sec ±2.90% (90 runs sampled)
184+
* #2: html-entities.decode - xml, body x 617,321 ops/sec ±0.74% (83 runs sampled)
185+
* #3: html-entities.decode - xml, attribute x 611,598 ops/sec ±0.50% (92 runs sampled)
186+
* #4: html-entities.decode - xml, strict x 607,191 ops/sec ±2.30% (85 runs sampled)
184187
185188
Escaping
186189
187190
Escape test
188191
189-
#1: entities.escapeUTF8 x 1,308,013 ops/sec ±0.37% (91 runs sampled)
190-
* #2: html-entities.encode - xml, specialChars x 1,258,760 ops/sec ±1.00% (93 runs sampled)
191-
#3: he.escape x 822,569 ops/sec ±0.24% (94 runs sampled)
192-
#4: entities.escape x 434,243 ops/sec ±0.34% (91 runs sampled)
192+
#1: entities.escapeUTF8 x 1,930,874 ops/sec ±0.80% (95 runs sampled)
193+
#2: he.escape x 1,717,522 ops/sec ±0.75% (84 runs sampled)
194+
* #3: html-entities.encode - xml, specialChars x 1,611,374 ops/sec ±1.30% (92 runs sampled)
195+
#4: entities.escape x 673,710 ops/sec ±1.30% (94 runs sampled)
193196
```
194197

195198
License

benchmark/benchmark.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-require-imports */
12
import * as Benchmark from 'benchmark';
23
import * as entities from 'entities';
34
import * as he from 'he';
@@ -16,14 +17,17 @@ type GlobalWithRequire = typeof global & {require: typeof require};
1617

1718
function cleanNodeModulesCache() {
1819
const cache = (global as GlobalWithRequire).require.cache;
19-
for (const cacheKey of Object.keys(cache)) {
20+
for (const cacheKey in cache) {
2021
delete cache[cacheKey];
2122
}
2223
}
2324

2425
function createRequireTests(modules: Record<string, string>) {
2526
return Object.keys(modules).reduce((result, moduleName) => {
26-
result[moduleName] = () => require(modules[moduleName]);
27+
result[moduleName] = function() {
28+
require(modules[moduleName]);
29+
cleanNodeModulesCache();
30+
};
2731
return result;
2832
}, {} as Record<string, () => void>);
2933
}
@@ -132,14 +136,14 @@ function section(sectionName: string, callback: () => void) {
132136
const indent = ' ';
133137
const indentWithPointer = ' * ';
134138

135-
function benchmark(name: string, tests: {[key: string]: () => void}, setup?: () => void) {
139+
function benchmark(name: string, tests: {[key: string]: () => void}) {
136140
console.log(`${indent}${name}\n`);
137141
const suite = new Benchmark.Suite();
138142
for (const [testName, testCallback] of Object.entries(tests)) {
139143
if (!includeOldTests && testName.indexOf('(old)') !== -1) {
140144
continue;
141145
}
142-
suite.add(testName, testCallback, {setup});
146+
suite.add(testName, testCallback);
143147
}
144148
suite.on('complete', function (this: Benchmark.Suite) {
145149
const benchmarks = Array.from((this as unknown) as Benchmark[]);
@@ -160,11 +164,11 @@ section('Common', () => {
160164
benchmark(
161165
'Initialization / Load speed',
162166
createRequireTests({
163-
'html-entities': '..',
167+
'html-entities': '../lib',
164168
entities: 'entities',
165169
he: 'he'
166170
}),
167-
cleanNodeModulesCache
171+
// cleanNodeModulesCache
168172
);
169173
});
170174
section('HTML5', () => {

0 commit comments

Comments
 (0)