tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.

Home Page:https://j2html.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Appendable performance

kicktipp opened this issue · comments

In DomContent the method render(Appendable writer) is already implemented and the method render() is abstract. This should be switched for more performance.

In ContainerTag for example the method looks like this:

 
    @Override
    public void render(Appendable writer) throws IOException {
        writer.append(renderOpenTag());
        if (children != null && !children.isEmpty()) {
            for (DomContent child : children) {
                child.render(writer);
            }
        }
        writer.append(renderCloseTag());
    }

But it would be much easier to implement it like this:

   @Override
    public void render(Appendable writer) throws IOException {
        renderOpenTag(writer);
        if (children != null && !children.isEmpty()) {
            for (DomContent child : children) {
                child.render(writer);
            }
        }
        renderCloseTag(writer);
    }

The difference is that in the first method each child is creating and returning a String object while the second method just appends to the same Appendable object in the rendering process.

Otherwise we are just glueing Strings together instead of taking advantage of a StringBuilder.

I will send a PR later on and will add some performance tests, too. Stay tuned...