Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Nice! A welcome addition!

Too bad they're not using KaTeX [0] instead.

It renders the maths server-side, so there's no runtime needed.

An additional bonus is that the resulting math is copy-pasteable, which in the case of disply math might not be that useful (since most equations are to complex to be meaningfully copy-pasted with unicode), but it helps from inline math dissappearing when copy pasting texts.

But, that being said, I'm sure they had their reasons to do so. For one, MathJax seems more well-known by quite a bit so maybe it's the safer option.

1: https://katex.org/



If I was building this feature, I'd also pick client side rendering, simply because at GitHub's scale, rendering on the server side will require a bunch of servers, which will need to be managed and looked after.

By offloading the rendering to the client, you make use of the spare capacity that exists on most client machines today, with no noticable slowdown for the user (it may even end up doing the "first paint" faster in the browser if GitHub are careful about their implementation).

Even with my SaaS product, we try and do as much work on the client as possible to reduce our server requirements. If you're sensible about it, users don't even notice.


> while no noticable slowdown for the user (it may even end up doing the "first paint" faster in the browser if GitHub are careful about their implementation).

Have you visited math.stackexchange? Pop by their MathJax reference page[0], and observe how long all the mathematical notation takes to render fully—it takes at least six seconds on my recent notebook, plugged in. On my 2018 iPad Pro, it takes well over thirty seconds on the first page load (drops to ~5 s on subsequent visits: there's probably some caching going on).

Here's[1] a benchmark comparing KaTeX (server-side), MathJax 2.7, and MathJax 3.0 (apparently a complete rewrite supporting server-side rendering[2], but it's still noticeably slower than KaTeX).

MathJax is really slow (slower still than LaTeX itself, and that's saying something).

[0]: https://math.meta.stackexchange.com/questions/5020/mathjax-b...

[1]: https://www.intmath.com/cg5/katex-mathjax-comparison.php

[2]: https://docs.mathjax.org/en/latest/upgrading/whats-new-3.0.h...


On my machine the first link loaded in under a second. On my mobile it was about 3 seconds, but it didn't feel especially sluggish. Even then, I'd say stackexchange has a somewhat inefficient implementation, in that they're trying to render everything in one shot.

For example, I see no reason why they wouldn't just render the stuff that's in view first, and the rest is rendered incrementally as the user scrolls, or a few seconds after the initial paint.

I take your point that KaTeX is faster in absolute terms, but from a usability point of view, the benefits do not outweigh the costs in my opinion.


> the first link loaded in under a second. On my mobile it was about 3 seconds

Did you wait for the typefaces to change to Computer Modern? On my smartphone, this took ages, and that is what I implied by 'fully loaded'; more complex maths is unreadable on some devices with only the native typefaces. Even a (relatively straightforward) cube root doesn't look clean[0].

MathJax has (from my observation) two 'levels' of rendering, where it uses the OS native typeface for a first pass, and then renders everything in Computer Modern for a uniform look (except on Apple devices, which somehow override this with STIX fonts).

Needless to say, the typeface change means that the dimensions of the maths content change, causing the rest of the website to reflow a second time. This second render takes the bulk of the time, and having a website's content unexpectedly jump around a long while after it has (apparently) loaded is not exactly user-friendly.

[0]: https://imgur.com/a/CrKqeI5


I wonder about your setup. On an iPhone 12 on residential wifi behind a VPN, I noticed no lag, delayed rendering, or weird rendering issues.


I can confirm that MathJax is very slow, especially if you render complex math including graphs (all in view). The Google Lighthouse Benchmark gave me a very bad performance score, but as soon as I switched to Katex my statically rendered blog gets 95-100 points for performance.


> [...] observe how long all the mathematical notation takes to render fully—it takes at least six seconds on my recent notebook, plugged in.

Does this really matter though? When I've encountered math-heavy pages that have been slow to fully render the math has also been slow for my brain to processes. As long as the math rendering is faster than my brain's math understanding it has been fine.


On my iPad Pro (2018), the first page rendered fully within seconds. I’d never visited the page before. I scrolled down and up the entire page to check.


On my mobile, for [1] KaTex loads in 900ms, MathJax in 1200-1600ms. Good enough for client side usage.


The tradeoff is exactly noticeable slowdown: https://imgur.com/a/y47haf9. Browser JS engines are single threaded, so MathJax has to wait its turn behind more important scripts, and it gets worse with slower devices and networks. It's a contest of download vs execution time, which a 2KB pre-rendered image will always win.

You're right on the money with the server costs though.


> Browser JS engines are single threaded

The JS interpreter is single threaded. But, you can offload the work to a web worker, that runs another instance of the interpreter in a separate thread.

Also for rendering LaTex it’s possible to optimize the rendering algorithm using WASM.

I’m not saying that MathJax does that, or it has good performance. But, there are options to optimize the client side rendering.


Workers lack document access and have to schedule updates with the main thread. That puts us back where we started (plus the overhead of starting the web worker).

I don't know enough about the state of WASM today to say the same. Circa 2018 I know that it lacked DOM manipulation.

(In some distant year I will figure out the incantation to avoid getting sniped when talking about this stuff. I think I rewrote that line 4 times.)


It doesn't put you back where you started at all. One thread aggregating the results from other workers and applying them to the DOM is not at all the same as a single thread doing all that work on its own.


Let me rephrase: distributing work to web workers won’t be faster in first page load than images in this conversation.

Because we have to participate in the main thread either way, prerendered images will always paint faster. There is simply too much action in the thread during the initial GitHub page lifecycle. The 40-50ms (~one long task) cost of spinning up a new web worker just solidifies that.


Sorry, but I find this approach very rude to your customers. It's kinda your job to make it work on your side and don't transfer the burdens to clients.

I don't know what your SAAS does (maybe it's not really applicable here), but in the case of github, it's very wastefull. Yes, it would take some work, some servers, etc but they have the skill, the money and everything they need to do it. They could do it once (well, with every change, but it's not very often) on their side instead every client on every pageview will have to do the same work again and again, wasting time and energy.


Interesting point of view, but I feel you're missing the point.

Do you find all native applications rude to customers?


No. I'm absolutely expecting local native app to do all the work, it makes sense. With SAAS running in the browser I expect all the heavy lifting is done somewhere else...you know, as a service.


Historically client-side rendering has been a big headache. Not sure if it's improved recently, but avid math bloggers and blog readers will remember the problems of the past decade.


As someone with no experience in the area, what have been the problems of the past decade?


I don't know why, but the rendering just wasn't that reliable. Equations failed to render too frequently.


I would probably pretender it.

Why?

Because markdown files seldom change and probably read much more often.

Anyway I would not agree that it is a good idea to move everything to client just because you can. There are always up and downsides.

I think your comment is too generic.


So use KaTeX on the client side. It's still much faster than MathJax.


Yes, agreed. I was just saying that client side rendering makes more sense; which library you choose is up to you.


For pages with lots of mathematics markup, it is far better (in terms of download size) to send the latex markup and the katex library to the browser, and render it there. I tried rendering the mathematics server-side using katex on my own website a while ago, and the div soup generated by katex takes up loads of space.

Original page (compressed): 10 kB

Page with server-rendered Katex (compressed): 50 kB

Katex.js (compressed): 80 kB

So after two pages it’s a net win to not render the mathematics server-side.


For my blog (e.g. https://daniel.lawrence.lu/blog/y2021m09d08/ with tons of math), I render the math serverside using MathJax and serve them as SVG images (with alt text for the visually impaired). They get cached too which is nice especially since many symbols are duplicated. Seems fast enough for me.


You're being modest. This is easily the most performant option... although it means losing interactivity (right clicking equations on GitHub allows copy to clipboard, A11y features, etc).

Running the JS client side, like GitHub, means blocking the thread. You're either going to be 1. delaying other JS from running, or 2. rendering late, shifting the layout – which is what GitHub has chosen: https://imgur.com/a/y47haf9

(Sending the rendered div's is a non-starter. Large document sizes delay domContentLoaded, slow down browsers, aren't shared cacheable resources, etc.)

Your approach, then. On your page there are 178 SVGs. Total gzipped size is 490KB. SVGO[0] gets that down to 311KB – that's 1.74KB transferred per equation. These are non-blocking, immutable, cacheable assets. Brilliant.

Upgrades:

- Figure out viewbox and inline height/width values on the HTML so no layout jank (aka "Cumulative Layout Shift"/CLS) occurs. Unsure if this is possible for inline math.

- Add `loading="lazy"` afterwards. Users that don't scroll the entire length of the page won't suffer unneeded downloads, and the inlined sizes will prevent late CLS for those that do.

- Maybe re-add interactivity? Cheap option: just support copying alt-text in a context menu.

[0] https://github.com/svg/svgo


> Figure out viewbox and inline height/width values on the HTML so no layout jank (aka "Cumulative Layout Shift"/CLS) occurs. Unsure if this is possible for inline math.

That's a great idea, I should do that. Right now I apply the height and offsets from the SVG file (via vertical-align and height) so that it flows nicely but I should add width too. It is trivially doable since the SVG does contain the width.

e.g.

    <img src="//daniel.lawrence.lu/texcache/683524188b1547a2a4466541ff07f2d6f83f599bi.svg" alt="\mathbf R(\mathbf T)" style="vertical-align: -0.838ex;height:2.843ex;">


Did you open-source the source code for trie DS visualization in your Aho-Corasick string matching algorithm blog?


Feel free to right click and view source and use it for whatever. I'll release it on Github with an MIT license when I get to my computer later...


I used to think KaTeX was far superior to MathJax but now I'm not so sure. I made https://mk12.github.io/web-math-demo/ to compare them and other things. They're definitely superior to browser MathML rendering today, which is nonexistent in Chrome (though see https://mathml.igalia.com/), quite bad in Safari, and OK in Firefox. It's true pre-rendering KaTeX produces a lot of markup, but it compresses very well so I don't think it's a big deal. They both have MathML for accessibility, but MathJax is more flexible in letting the user right-click and change the rendering engine, view raw TeX, etc. Rendering KaTeX on the client side is faster than MathJax, but in my experience KaTeX is slightly worse quality, e.g. https://github.com/KaTeX/KaTeX/issues/3400 has gone unfixed for a long time.


There's currently around 100 LaTeX functions listed as "Not supported" in the Katex docs at https://katex.org/docs/support_table.html I've been trying hard for a while with my site (https://cocalc.com) to use only katex, but that's definitely never going to happen. Users frequently hit missing functionality, e.g., they have lots of notebooks that use "\mbox", so it's critical to support full mathjax. I currently do this by attempting to use katex, then falling back to mathjax if it fails. That said, as you point out, mathjax has massively improved over the last few years with mathjax3! Thanks for pointing out the quality issues, which I hadn't thought about.


> https://github.com/KaTeX/KaTeX/issues/3400 has gone unfixed for a long time.

I don't think I'd qualify an issue from the end of last year as "unfixed for a long time".


Sorry I posted the wrong issue, it’s actually a duplicate of https://github.com/KaTeX/KaTeX/issues/3168 which was filed last August. I guess it’s not that long, but it was annoying for my use of KaTeX on https://mitchellkember.com/notes4u where I see it frequently. For example it affects \left\lvert\vec{b}\right\rvert which is a pretty simple piece of TeX I’d expect to render fine.


The last I knew, KaTeX had issues with accessibility. But honestly it is probably just best to ship raw MathML, translated from LaTeX on the server, and use MathJax as a polyfill for Chrome and Edge users. That way you get extremely fast rendering in Safari and Firefox without any extra javascript, and eventually Chrome will catch up.


I wouldn’t recommend relying on Safari MathML rendering today for anything but very basic equations. Firefox is a lot better, but still not quite at MathJax quality. I made a demo here https://mk12.github.io/web-math-demo/ that lets you compare them. Also if you edit the MathML markup box, it will render directly from that, just like the poly fill (sometimes the layout shifts slightly when switching from TeX -> MathJax HTML+CSS to TeX -> MathML -> MathJax HTML+CSS).


Nice demo. I don’t have access to a mac so I can’t see this in Safari, but using Firefox I can immediately spot there are some kerning mistakes in the MathML rendering which are not present in MathJax rendering.

However, isn’t that just the fact the the default math font (DejaVu Math TeX Gyre in Firefox i-on Ubuntu) is not good enough? I use Libertinus Math in my own little demo page (https://runarberg.github.io/mathup/) and it looks just fine (IMO). The only think to note is that TeX Gyre is so pervasive (as it is the default [only?] MathJax font) that it takes a bit getting used to other math fonts on the web.

For example here is the Iglalia example from your demo page displayed with Libertinus Math in Firfox (https://imgur.com/q3QuJOA). The same example has some issues in your demo page with DejaVu Math TeX Gyre


> I'm sure they had their reasons

They do, accessibility being one of them. Rendering math in the way you describe makes it difficult, or impossible, to understand for all sorts of audiences, including those relying on screen reading software.


KaTeX includes hidden MathML for screen readers.

https://github.com/KaTeX/KaTeX/issues/38


Our GitLab docs state "Math written in LaTeX syntax is rendered with KaTeX" https://docs.gitlab.com/ee/user/markdown.html but I'm not sure it isn't just using MathJax under the hood.


MathJax can do server side rendering too now!


Maths don't scale up. Moore's Law will catch up and eventually rendering math will be like rendering jpg. For now the priority is to get things to work so we don't waste extra time determining if we just avoid math or we use stupid PNG.


I'm so happy that I can just paste my definitions from latex preamble into a paragraph to include all the calitographic symbols and other conveniences with Mathjax. As far as I know, I can't easily do it without adapting to Katex-specific syntax.


Everything else on a web page is rendered client side (HTML is just text…) so client side rendering makes sense to me here too.


Rendering HTML doesn't require a huge JS library.


HTML requires a huge browser binary though, and the JS library should be cached on first visit to a page that uses it. Seems the right trade off to me to have a one off download for this use case (i.e. potentially viewing many pages on the same site, as opposed to a small number of pre rendered pages) then only need to transfer plain text which is already in the markdown source (so is needed if you’re going to edit or view the raw MD anyway).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: