r/javascript • • 27d ago

A Kanji lookup table, five commands, and 36% less Base64

https://github.com/datalog/kanji-base64
1 Upvotes

7 comments sorted by

2

u/wowokdex 27d ago

It seems like you get similar amounts of compression with gzip. The readme suggests that it's for in-source kanji but wouldn't any distributed js be gzipped already? I'm guessing if you compress with this and then gzip the compression doesn't stack.

1

u/erodriguez-3 5d ago

Does the table expand at build time or runtime? Runtime decoding would trade bytes for startup work.

1

u/b00mandbum 3d ago

It expands at runtime, once on the first call, and then stays cached in memory.

You're right about the trade-off, it trades bundle size for a bit of initial CPU work. Decoding ~10.5 KB of byte commands into an 8,192-entry array takes less than a millisecond in modern JS engines, and there is zero cost at module load if you never call it.

It also works well with gzip because it removes big jumps between Unicode blocks first. Even after gzip, it still saves around 29% compared to the raw table.

1

u/pmickael_584 2d ago

Benchmark first-call latency separately from cached lookups; they exercise different paths.

1

u/b00mandbum 27d ago

I ended up with five commands:

CommandWhat it does
A(value)Write one full value
D(delta)Add a difference to the previous nonempty value
E(count)Fill a run of empty positions
R(start, step, count)Write a series from a given starting value
P(step, count)Continue from the previous nonempty value with a fixed step

The decoder follows those instructions to reconstruct the mapping.

A regular stretch: 40 bytes to 5

ぁあぃいぅうぇえぉおかがきぎくぐけげこご

These 20 characters have consecutive Unicode values. We can describe the whole stretch with one command:

R(0x3041, +1, 20)

Start at 0x3041, add one each time, emit 20 values. 40 bytes become 5.

A mixed stretch: 40 bytes to 25 (less dramatic)

” ( ) 〔 〕 [ ] { } 〈 〉 《 》 「 」 『 』 【 】 +

These 20 characters need a mix of commands:

A(0x201D)
A(0xFF08)
D(+1)
A(0x3014)
D(+1)
A(0xFF3B)
D(+2)
D(+30)
D(+2)
R(0x3008, +1, 10)
A(0xFF0B)

We store some values in full, use small deltas where we can, and describe ten consecutive values with one series command. 40 bytes become 25.