Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance issue because of lots of nested layers #78

Open
mapleeit opened this issue May 6, 2020 · 4 comments
Open

Performance issue because of lots of nested layers #78

mapleeit opened this issue May 6, 2020 · 4 comments

Comments

@mapleeit
Copy link
Contributor

mapleeit commented May 6, 2020

Use the example on readme:

var Convert = require('ansi-to-html');
var convert = new Convert();

convert.toHtml('\x1b[30mblack\x1b[37mwhite');

this will generated a html structure like this:

<span>
  black
  <span>
    white
  </span>
</span>

But I think it's better to generate a html like this:

<span>
  black
</span>
<span>
  white
</span>

Lots of unnecessary nested layers will slow down the browser. Another package ansi-html doesn't have this problem.

@mapleeit
Copy link
Contributor Author

Could place this file to the root and execute.

"use strict";

const fs = require("fs");
const Convert = require("./lib/ansi_to_html");

const convert = new Convert();

let content = "";

for (let i = 0; i < 10000; i++) {
    content += '\x1b[30mblack\x1b[37mwhite\r\n\r\n';
}

fs.writeFileSync("./bench.html", `
<html>
    <body>
        ${convert.toHtml(content)}
    </body>
</html>
`);

And will get a html file bench.html. Open it with a browser and will see the terrible performance

@rburns rburns mentioned this issue Aug 26, 2020
@rburns
Copy link
Owner

rburns commented Aug 26, 2020

thanks for writing a test for that. There have been pending changes that I thought might make that kind of problem worse, but have just been speculating. #53 (comment) #59 (comment)

@brettz9
Copy link
Contributor

brettz9 commented Aug 26, 2020

If you want to be more faithful to how ANSI works, e.g., so as to use the "undo"-type commands to undo a previous style if applied, you really have to use nesting. (And to support text-decoration, specifically, switching to <a> as per #75 should be necessary also.)

However, you might be able to find an HTML/CSS cleaner which optimizes the final result to potentially avoid some redundancy. A quick search of npm though didn't turn up useful results for me.

@TimoHocker
Copy link

If you want to be more faithful to how ANSI works, e.g., so as to use the "undo"-type commands to undo a previous style if applied, you really have to use nesting.

you could also just close the tag of the previous applied style to 'undo' it. Text decoration might require nesting, depending on how you want to solve it, but not to billions of layers. Some additional logic could just close the previous tag, figure out the combined style of the next section and open a new tag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants