tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.
https://j2html.com/
Apache License 2.0
765 stars 136 forks source link

Appendable performance #86

Closed kicktipp closed 6 years ago

kicktipp commented 6 years ago

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...