0

I want to write a tinyurl clone, and take text like:

https://myfunwebsite.com/coolthing

and shorten that string to something like:

asdf9SDFs23vbk

I have the following code:

const buffer = zlib.deflateSync(myString);
const compressed = buffer.toString('ascii');

But the problem is it creates a string like x?K())0RWOHMII/O/JIQKL??O??? which is bad for putting into url params. I want the resulting string to only be letters and numbers, how do I do this?

Mirror318
  • 11,875
  • 14
  • 64
  • 106
  • 1
    you could base64 (or similar) the result, but a more traditional implementation would be to store the short strings in database along with their matching urls, instead of relying on decoding/encoding. – IT goldman Nov 15 '22 at 22:58
  • Base64 won't compress though, it just encodes, and your result will likely take more space than the url initial size given you have less characters available in base64 than in utf-8 – user3252327 Nov 15 '22 at 23:03
  • 2
    I think your miss understanding how tinyURL works, it's not compressing urls, it's storing them, and then just returning an ID.. – Keith Nov 15 '22 at 23:06

1 Answers1

2

Um, no. Even if you didn't have to worry about arbitrary bytes in a URL, you're not going to be able to compress a short string like "https://myfunwebsite.com/coolthing". Those 34 characters will get expanded into 50 to 100 bytes by the best compressors. Lossless compressors need a lot more data than that to get rolling.

Then if you encoded it into URL-safe characters, you've expanded it by another 25% to 33%, so now you're at around 60 to 140 characters from the original 34.

This is why TinyURL doesn't work the way you seem to think it does. It does not compress. It stores the full URL on its server, and then gives you a code with which it can find that stored URL and play it back to you.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158