Skip to content

victrme/slim-htmlparser2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slim-htmlparser2

NPM Version JSR Version Downloads

The fast & forgiving HTML parser, only with Parser.

htmlparser2 is the fastest HTML parser, and takes some shortcuts to get there.

Usage

htmlparser2 itself provides a callback interface that allows consumption of documents with minimal allocations.

import * as htmlparser2 from '@victr/htmlparser2'

const parser = new htmlparser2.Parser({
  onopentag(name, attributes) {
    if (name === 'script' && attributes.type === 'text/javascript') {
      console.log('JS! Hooray!')
    }
  },
  ontext(text) {
    console.log('-->', text)
  },
  onclosetag(tagname) {
    if (tagname === 'script') {
      console.log("That's it?!")
    }
  },
})
parser.write("Xyz <script type='text/javascript'>const foo = '<<bar>>';</script>")
parser.end()

Output (with multiple text events combined):

--> Xyz
JS! Hooray!
--> const foo = '<<bar>>';
That's it?!

Performance

htmlparser2        : 2.17215 ms/file ± 3.81587
node-html-parser   : 2.35983 ms/file ± 1.54487
html5parser        : 2.43468 ms/file ± 2.81501
neutron-html5parser: 2.61356 ms/file ± 1.70324
htmlparser2-dom    : 3.09034 ms/file ± 4.77033
html-dom-parser    : 3.56804 ms/file ± 5.15621
libxmljs           : 4.07490 ms/file ± 2.99869
htmljs-parser      : 6.15812 ms/file ± 7.52497
parse5             : 9.70406 ms/file ± 6.74872
htmlparser         : 15.0596 ms/file ± 89.0826
html-parser        : 28.6282 ms/file ± 22.6652
saxes              : 45.7921 ms/file ± 128.691
html5              : 120.844 ms/file ± 153.944