Quickly detecting JSON Escapes with SWAR

typescript dev.to

Most JSON string data is boring. It is just ASCII text that does not contain " or \, does not dip below 0x20, and does not force any special-case escaping at all. The annoying part is that a serializer still has to prove that. The naive loop is obvious: for (let i = 0; i src.length; i++) { const code = src.charCodeAt(i); if (code == 34 || code == 92 || code 32) { // escape } } That is correct, but it is also one branch per code unit in the hottest part of the serializer.

Read Full Tutorial open_in_new
arrow_back Back to Tutorials