There is no optimal length, it depends on the content of your page, but usually, H2 tags are longer than H1 tags because that they describe the subheadings regarding H1 and it's better to use H1 to emphasize important text, for example
<h1>Optimal length of H1 and H2 for SEO</h1>
<h2>Why does it matter to know the optimal length for H1 and H2?</h2>
<h2>How different lengths of H1 and H2 affects SEO?</h2>
But it's always better to make H2 tags short and H1 tags shorter and to the point, don't stuff it with unnecessary words because that will negatively affect your SEO.
For example, if someone searches for
, and there are 2 equally SEO websites, except for that
one has the H1 as
the other one has the H1 as
<h1>Foo Bar Baz</h1>
Site number 1 will be first because 100% of keywords are Foo, but if it was Foo Bar Baz 33% will be to each keyword (regarding the heading only).
Another length factor rule is the content of your website.
Having keywords in the header which are repeated several times in your content is a good practice.
<p>200 words of content is a good practice
regarding foo bar because 200 words of content foo
200 words of content bar</p>
The only thing that Google explicitly mentioned regarding headings length is to not use Very long headings.