Menu

1 / 4

I Cut 1,000 Strings to 20 Characters With slice(). 136 Came Out Broken.

JavaScript in Plain English - Medium·Ferid Brković·about 5 hours ago
#NexwlYhn
Reading 0:00
15s threshold

Member-only story

I Cut 1,000 Strings to 20 Characters With slice(). 136 Came Out Broken.

Emoji, accents, Hindi, Thai and Arabic through four truncation methods: the counts, the one that broke none, and its cost.

Ferid Brković

8 min read2 hours ago

Press enter or click to view image in full size

'Family: 👨👩👧👦'.slice(0, 10) returns 'Family: 👨' . A family of four went in, one man came out, and nothing threw. Ask for nine characters instead and you get 'Family: �' .

That is one emoji. I wanted a number, so I built 1,000 strings of the kind a web app shortens every day, cut each of them with the four methods developers reach for, and counted how many came out damaged.

The short version: at 20 characters, slice() broke 136 of the 1,000. The popular "just spread it" fix rescued 19 of those. One method broke none, and I timed what it costs.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.