atom / highlights

Syntax highlighter
https://atom.github.io/highlights
MIT License
529 stars 54 forks source link

The amount of <span> elements can impact rendering performance #70

Open janko opened 5 years ago

janko commented 5 years ago

Prerequisites

Description

I'm using this package separately from Atom, for syntax highlighting code snippets in markdown files. I've noticed that when I switched from Highlight.js/Prism to Highlights, my page rendering performance reduced.

This appears to be due to the number of <span> elements Highlights generates. In addition to the ones that have class attributes with token names, there are also a lot of <span> elements generated which don't have any class attribute.

When I tried stripping just the classless <span> tags, the rendering performance was now instant again.

Steps to Reproduce

const Highlights = require("highlights")

const highlighter = new Highlights()

const html = highlighter.highlightSync({
  fileContents: 'answer = 42',
  scopeName: 'source.ruby',
})

console.log(html)
<pre class="editor editor-colors">
  <div class="line">
    <span class="source ruby">
      <span>answer&nbsp;</span>
      <span class="keyword operator assignment ruby">
        <span>=</span>
      </span>
      <span>&nbsp;</span>
      <span class="constant numeric ruby">
        <span>42</span>
      </span>
    </span> 
  </div>
</pre>

Expected behavior:

That highlighting produces only the tags that are necessary.

Actual behavior:

Highlighting produces many <span> tags that are not needed (the script above produces 4 such tags).

Versions

Highlights 3.1.4

rsese commented 5 years ago

Thanks again @janko -

I'm using this package separately from Atom, for syntax highlighting code snippets in markdown files. I've noticed that when I switched from Highlight.js/Prism to Highlights, my page rendering performance reduced.

As I mentioned in https://github.com/atom/highlights/issues/69#issuecomment-545617638, can you confirm if this behavior causes a performance problem in Atom?

janko commented 5 years ago

Not that I know of, I don't use Atom, I've only experienced it when using the library standalone.