⌈⌋ ⎇ branch:  Bitrhythm


Hex Artifact Content

Artifact 738169062c158f57cf76d79226dc731b61c88c06e246c87e8ec7739a226df274:


0000: 23 20 6a 73 6f 6e 2d 75 72 6c 0a 0a 5b 21 5b 6e  # json-url..[![n
0010: 70 6d 20 64 6f 77 6e 6c 6f 61 64 73 5d 5b 64 6f  pm downloads][do
0020: 77 6e 6c 6f 61 64 73 2d 69 6d 61 67 65 5d 5d 5b  wnloads-image]][
0030: 64 6f 77 6e 6c 6f 61 64 73 2d 75 72 6c 5d 20 5b  downloads-url] [
0040: 21 5b 42 75 69 6c 64 20 53 74 61 74 75 73 5d 5b  ![Build Status][
0050: 74 72 61 76 69 73 2d 69 6d 61 67 65 5d 5d 5b 74  travis-image]][t
0060: 72 61 76 69 73 2d 75 72 6c 5d 20 5b 21 5b 44 65  ravis-url] [![De
0070: 70 65 6e 64 65 6e 63 79 20 53 74 61 74 75 73 5d  pendency Status]
0080: 5b 64 61 76 69 64 64 6d 2d 69 6d 61 67 65 5d 5d  [daviddm-image]]
0090: 5b 64 61 76 69 64 64 6d 2d 75 72 6c 5d 20 5b 21  [daviddm-url] [!
00a0: 5b 43 6f 76 65 72 61 67 65 20 53 74 61 74 75 73  [Coverage Status
00b0: 5d 5b 63 6f 76 65 72 61 67 65 2d 69 6d 61 67 65  ][coverage-image
00c0: 5d 5d 5b 63 6f 76 65 72 61 67 65 2d 75 72 6c 5d  ]][coverage-url]
00d0: 0a 0a 47 65 6e 65 72 61 74 65 20 55 52 4c 2d 73  ..Generate URL-s
00e0: 61 66 65 20 72 65 70 72 65 73 65 6e 74 61 74 69  afe representati
00f0: 6f 6e 73 20 6f 66 20 73 6f 6d 65 20 61 72 62 74  ons of some arbt
0100: 69 72 61 72 79 20 4a 53 4f 4e 20 64 61 74 61 20  irary JSON data 
0110: 69 6e 20 61 73 20 73 6d 61 6c 6c 20 61 20 73 70  in as small a sp
0120: 61 63 65 20 61 73 20 70 6f 73 73 69 62 6c 65 20  ace as possible 
0130: 74 68 61 74 20 63 61 6e 20 62 65 20 73 68 61 72  that can be shar
0140: 65 64 20 69 6e 20 61 20 62 6f 6f 6b 6d 61 72 6b  ed in a bookmark
0150: 20 2f 20 6c 69 6e 6b 2e 0a 0a 41 6c 74 68 6f 75   / link...Althou
0160: 67 68 20 64 65 73 69 67 6e 65 64 20 74 6f 20 77  gh designed to w
0170: 6f 72 6b 20 69 6e 20 4e 6f 64 65 2c 20 61 20 73  ork in Node, a s
0180: 74 61 6e 64 61 6c 6f 6e 65 20 63 6c 69 65 6e 74  tandalone client
0190: 2d 73 69 64 65 20 6c 69 62 72 61 72 79 20 69 73  -side library is
01a0: 20 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 63   provided that c
01b0: 61 6e 20 62 65 20 75 73 65 64 20 64 69 72 65 63  an be used direc
01c0: 74 6c 79 20 6f 6e 20 74 68 65 20 62 72 6f 77 73  tly on the brows
01d0: 65 72 2e 0a 0a 23 23 20 55 73 61 67 65 0a 0a 23  er...## Usage..#
01e0: 23 23 20 43 6f 6d 70 72 65 73 73 0a 0a 60 60 60  ## Compress..```
01f0: 0a 09 76 61 72 20 63 6f 64 65 63 20 3d 20 72 65  ..var codec = re
0200: 71 75 69 72 65 28 27 6a 73 6f 6e 2d 75 72 6c 27  quire('json-url'
0210: 29 28 27 6c 7a 77 27 29 3b 0a 09 76 61 72 20 6f  )('lzw');..var o
0220: 62 6a 20 3d 20 7b 20 6f 6e 65 3a 20 31 2c 20 74  bj = { one: 1, t
0230: 77 6f 3a 20 32 2c 20 74 68 72 65 65 3a 20 5b 31  wo: 2, three: [1
0240: 2c 32 2c 33 5d 2c 20 66 6f 75 72 3a 20 27 72 65  ,2,3], four: 're
0250: 64 20 70 69 6e 65 61 70 70 6c 65 73 27 20 7d 3b  d pineapples' };
0260: 0a 09 63 6f 64 65 63 2e 63 6f 6d 70 72 65 73 73  ..codec.compress
0270: 28 6f 62 6a 29 2e 74 68 65 6e 28 72 65 73 75 6c  (obj).then(resul
0280: 74 20 3d 3e 20 63 6f 6e 73 6f 6c 65 2e 6c 6f 67  t => console.log
0290: 28 72 65 73 75 6c 74 29 29 3b 0a 09 2f 2a 20 52  (result));../* R
02a0: 65 73 75 6c 74 3a 20 77 6f 54 43 6f 32 39 75 5a  esult: woTCo29uZ
02b0: 51 48 43 6f 33 52 33 62 77 4c 43 70 58 52 6f 63  QHCo3R3bwLCpXRoc
02c0: 6d 56 6c 77 70 4d 42 41 67 50 43 70 47 5a 76 64  mVlwpMBAgPCpGZvd
02d0: 58 4c 43 72 73 53 4f 5a 43 42 77 61 63 53 44 59  XLCrsSOZCBwacSDY
02e0: 58 42 77 62 47 56 7a 20 2a 2f 0a 60 60 60 0a 0a  XBwbGVz */.```..
02f0: 23 23 23 20 44 65 63 6f 6d 70 72 65 73 73 0a 0a  ### Decompress..
0300: 60 60 60 0a 09 76 61 72 20 63 6f 64 65 63 20 3d  ```..var codec =
0310: 20 72 65 71 75 69 72 65 28 27 6a 73 6f 6e 2d 75   require('json-u
0320: 72 6c 27 29 28 27 6c 7a 6d 61 27 29 3b 0a 09 63  rl')('lzma');..c
0330: 6f 64 65 63 2e 64 65 63 6f 6d 70 72 65 73 73 28  odec.decompress(
0340: 73 6f 6d 65 43 6f 6d 70 72 65 73 73 65 64 53 74  someCompressedSt
0350: 72 69 6e 67 29 2e 74 68 65 6e 28 6a 73 6f 6e 20  ring).then(json 
0360: 3d 3e 20 7b 20 2f 2a 20 6f 70 65 72 61 74 65 20  => { /* operate 
0370: 6f 6e 20 6a 73 6f 6e 20 2a 2f 20 7d 29 0a 60 60  on json */ }).``
0380: 60 0a 0a 23 23 23 20 53 74 61 74 73 0a 0a 60 60  `..### Stats..``
0390: 60 0a 09 76 61 72 20 63 6f 64 65 63 20 3d 20 72  `..var codec = r
03a0: 65 71 75 69 72 65 28 27 6a 73 6f 6e 2d 75 72 6c  equire('json-url
03b0: 27 29 28 27 6c 7a 73 74 72 69 6e 67 27 29 3b 0a  ')('lzstring');.
03c0: 09 63 6f 64 65 63 2e 73 74 61 74 73 28 6f 62 6a  .codec.stats(obj
03d0: 29 2e 74 68 65 6e 28 0a 09 09 28 7b 20 72 61 77  ).then(...({ raw
03e0: 65 6e 63 6f 64 65 64 2c 20 63 6f 6d 70 72 65 73  encoded, compres
03f0: 73 65 64 65 6e 63 6f 64 65 64 2c 20 63 6f 6d 70  sedencoded, comp
0400: 72 65 73 73 69 6f 6e 20 7d 29 20 3d 3e 20 7b 0a  ression }) => {.
0410: 09 09 09 63 6f 6e 73 6f 6c 65 2e 6c 6f 67 28 60  ...console.log(`
0420: 52 61 77 20 55 52 49 2d 65 6e 63 6f 64 65 64 20  Raw URI-encoded 
0430: 4a 53 4f 4e 20 73 74 72 69 6e 67 20 6c 65 6e 67  JSON string leng
0440: 74 68 3a 20 24 7b 72 61 77 65 6e 63 6f 64 65 64  th: ${rawencoded
0450: 7d 60 29 3b 0a 09 09 09 63 6f 6e 73 6f 6c 65 2e  }`);....console.
0460: 6c 6f 67 28 60 43 6f 6d 70 72 65 73 73 65 64 20  log(`Compressed 
0470: 55 52 49 2d 65 6e 63 6f 64 65 64 20 4a 53 4f 4e  URI-encoded JSON
0480: 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 3a 20   string length: 
0490: 24 7b 63 6f 6d 70 72 65 73 73 65 64 65 6e 63 6f  ${compressedenco
04a0: 64 65 64 7d 60 29 3b 0a 09 09 09 63 6f 6e 73 6f  ded}`);....conso
04b0: 6c 65 2e 6c 6f 67 28 60 43 6f 6d 70 72 65 73 73  le.log(`Compress
04c0: 69 6f 6e 20 72 61 74 69 6f 20 28 72 61 77 20 2f  ion ratio (raw /
04d0: 20 63 6f 6d 70 72 65 73 73 65 64 29 3a 20 24 7b   compressed): ${
04e0: 63 6f 6d 70 72 65 73 73 69 6f 6e 7d 60 29 3b 0a  compression}`);.
04f0: 09 09 7d 0a 09 29 3b 0a 60 60 60 0a 0a 23 23 23  ..}..);.```..###
0500: 20 53 74 61 6e 64 61 6c 6f 6e 65 20 42 72 6f 77   Standalone Brow
0510: 73 65 72 20 42 75 6e 64 6c 65 0a 0a 60 60 60 0a  ser Bundle..```.
0520: 3c 73 63 72 69 70 74 20 74 79 70 65 3d 22 74 65  <script type="te
0530: 78 74 2f 6a 61 76 61 73 63 72 69 70 74 22 20 73  xt/javascript" s
0540: 72 63 3d 22 2f 64 69 73 74 2f 62 72 6f 77 73 65  rc="/dist/browse
0550: 72 2f 6a 73 6f 6e 2d 75 72 6c 2e 6a 73 22 3e 3c  r/json-url.js"><
0560: 2f 73 63 72 69 70 74 3e 0a 3c 73 63 72 69 70 74  /script>.<script
0570: 3e 0a 09 63 6f 6e 73 74 20 6c 69 62 20 3d 20 4a  >..const lib = J
0580: 73 6f 6e 55 72 6c 28 27 6c 7a 6d 61 27 29 3b 20  sonUrl('lzma'); 
0590: 2f 2f 20 4a 73 6f 6e 55 72 6c 20 69 73 20 61 64  // JsonUrl is ad
05a0: 64 65 64 20 74 6f 20 74 68 65 20 77 69 6e 64 6f  ded to the windo
05b0: 77 20 6f 62 6a 65 63 74 0a 09 6c 69 62 2e 63 6f  w object..lib.co
05c0: 6d 70 72 65 73 73 28 70 61 72 73 65 64 29 2e 74  mpress(parsed).t
05d0: 68 65 6e 28 6f 75 74 70 75 74 20 3d 3e 20 7b 20  hen(output => { 
05e0: 72 65 73 75 6c 74 2e 76 61 6c 75 65 20 3d 20 6f  result.value = o
05f0: 75 74 70 75 74 3b 20 7d 29 3b 0a 3c 2f 73 63 72  utput; });.</scr
0600: 69 70 74 3e 0a 60 60 60 0a 0a 54 6f 20 73 65 65  ipt>.```..To see
0610: 20 69 74 20 69 6e 20 61 63 74 69 6f 6e 2c 20 64   it in action, d
0620: 6f 77 6e 6c 6f 61 64 20 74 68 65 20 73 6f 75 72  ownload the sour
0630: 63 65 20 63 6f 64 65 20 61 6e 64 20 72 75 6e 20  ce code and run 
0640: 60 6e 70 6d 20 72 75 6e 20 65 78 61 6d 70 6c 65  `npm run example
0650: 60 2c 20 6f 72 20 73 69 6d 70 6c 79 20 76 69 73  `, or simply vis
0660: 69 74 20 5b 74 68 69 73 20 6c 69 6e 6b 5d 28 68  it [this link](h
0670: 74 74 70 3a 2f 2f 6a 73 62 69 6e 2e 63 6f 6d 2f  ttp://jsbin.com/
0680: 63 61 79 75 68 6f 78 29 2e 0a 0a 2a 20 54 68 65  cayuhox)...* The
0690: 20 62 72 6f 77 73 65 72 20 62 75 6e 64 6c 65 20   browser bundle 
06a0: 69 73 20 67 65 6e 65 72 61 74 65 64 20 75 73 69  is generated usi
06b0: 6e 67 20 57 65 62 70 61 63 6b 20 61 6e 64 20 63  ng Webpack and c
06c0: 6f 6e 73 69 73 74 73 20 6f 66 20 6d 75 6c 74 69  onsists of multi
06d0: 70 6c 65 20 63 68 75 6e 6b 73 2c 20 77 69 74 68  ple chunks, with
06e0: 20 74 68 65 20 6d 61 69 6e 20 63 68 75 6e 6b 20   the main chunk 
06f0: 65 6e 74 72 79 20 70 6f 69 6e 74 20 6c 6f 63 61  entry point loca
0700: 74 65 64 20 61 74 20 60 64 69 73 74 2f 62 72 6f  ted at `dist/bro
0710: 77 73 65 72 2f 6a 73 6f 6e 2d 75 72 6c 2e 6a 73  wser/json-url.js
0720: 60 2e 20 43 68 75 6e 6b 73 20 6d 75 73 74 20 62  `. Chunks must b
0730: 65 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65  e located in the
0740: 20 73 61 6d 65 20 66 6f 6c 64 65 72 20 61 73 20   same folder as 
0750: 74 68 65 20 6d 61 69 6e 20 6d 6f 64 75 6c 65 20  the main module 
0760: 69 74 73 65 6c 66 2e 0a 2a 20 49 27 76 65 20 74  itself..* I've t
0770: 72 69 65 64 20 6d 79 20 62 65 73 74 20 74 6f 20  ried my best to 
0780: 72 65 64 75 63 65 20 74 68 65 20 62 75 6e 64 6c  reduce the bundl
0790: 65 20 73 69 7a 65 73 2c 20 62 75 74 20 74 68 65  e sizes, but the
07a0: 20 6d 6f 64 75 6c 65 20 28 61 74 20 6c 65 61 73   module (at leas
07b0: 74 20 74 68 65 20 65 6e 74 72 79 29 20 69 73 20  t the entry) is 
07c0: 73 74 69 6c 6c 20 73 75 72 70 72 69 73 69 6e 67  still surprising
07d0: 6c 79 20 6c 61 72 67 65 20 6f 6e 20 74 68 65 20  ly large on the 
07e0: 62 72 6f 77 73 65 72 20 28 35 36 6b 62 20 6d 69  browser (56kb mi
07f0: 6e 69 66 69 65 64 2c 20 32 30 6b 62 20 67 7a 69  nified, 20kb gzi
0800: 70 70 65 64 29 2e 20 4d 6f 73 74 20 6f 66 20 69  pped). Most of i
0810: 74 20 69 73 20 64 75 65 20 74 6f 20 74 68 65 20  t is due to the 
0820: 62 75 66 66 65 72 2e 6a 73 20 73 68 69 6d 2c 20  buffer.js shim, 
0830: 61 6e 64 20 70 61 72 74 6c 79 20 64 75 65 20 74  and partly due t
0840: 6f 20 74 68 65 20 72 65 67 65 6e 65 72 61 74 6f  o the regenerato
0850: 72 2d 72 75 6e 74 69 6d 65 20 2d 20 49 20 6d 61  r-runtime - I ma
0860: 79 20 72 65 76 69 73 69 74 20 74 68 69 73 20 6c  y revisit this l
0870: 61 74 65 72 20 74 6f 20 74 72 79 20 61 6e 64 20  ater to try and 
0880: 69 6d 70 72 6f 76 65 20 65 66 66 69 63 69 65 6e  improve efficien
0890: 63 79 2e 0a 0a 23 23 20 55 73 61 67 65 20 4e 6f  cy...## Usage No
08a0: 74 65 73 0a 0a 2a 20 41 6c 74 68 6f 75 67 68 20  tes..* Although 
08b0: 6e 6f 74 20 61 6c 6c 20 61 6c 67 6f 72 69 74 68  not all algorith
08c0: 6d 73 20 61 72 65 20 61 73 79 6e 63 68 72 6f 6e  ms are asynchron
08d0: 6f 75 73 2c 20 61 6c 6c 20 66 75 6e 63 74 69 6f  ous, all functio
08e0: 6e 73 20 72 65 74 75 72 6e 20 50 72 6f 6d 69 73  ns return Promis
08f0: 65 73 20 74 6f 20 65 6e 73 75 72 65 20 63 6f 6d  es to ensure com
0900: 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 20 49 6e  patibility..* In
0910: 73 74 61 6e 74 69 61 74 65 20 61 6e 20 69 6e 73  stantiate an ins
0920: 74 61 6e 63 65 20 77 69 74 68 20 61 70 70 72 6f  tance with appro
0930: 70 72 69 61 74 65 20 63 6f 6d 70 72 65 73 73 69  priate compressi
0940: 6f 6e 20 63 6f 64 65 63 20 62 65 66 6f 72 65 20  on codec before 
0950: 75 73 69 6e 67 2e 0a 2a 20 56 61 6c 69 64 20 63  using..* Valid c
0960: 6f 64 65 63 73 3a 0a 09 2a 20 6c 7a 77 0a 09 2a  odecs:..* lzw..*
0970: 20 6c 7a 6d 61 0a 09 2a 20 6c 7a 73 74 72 69 6e   lzma..* lzstrin
0980: 67 20 2d 20 72 75 6e 73 20 6c 7a 73 74 72 69 6e  g - runs lzstrin
0990: 67 20 61 67 61 69 6e 73 74 20 61 20 73 74 72 69  g against a stri
09a0: 6e 67 69 66 69 65 64 20 4a 53 4f 4e 20 69 6e 73  ngified JSON ins
09b0: 74 65 61 64 20 6f 66 20 75 73 69 6e 67 20 4d 65  tead of using Me
09c0: 73 73 61 67 65 50 61 63 6b 20 6f 6e 20 4a 53 4f  ssagePack on JSO
09d0: 4e 0a 09 2a 20 70 61 63 6b 20 2d 20 74 68 69 73  N..* pack - this
09e0: 20 6a 75 73 74 20 75 73 65 73 20 4d 65 73 73 61   just uses Messa
09f0: 67 65 50 61 63 6b 20 61 6e 64 20 63 6f 6e 76 65  gePack and conve
0a00: 72 74 73 20 74 68 65 20 62 69 6e 61 72 79 20 62  rts the binary b
0a10: 75 66 66 65 72 20 69 6e 74 6f 20 61 20 42 61 73  uffer into a Bas
0a20: 65 36 34 20 55 52 4c 2d 73 61 66 65 20 72 65 70  e64 URL-safe rep
0a30: 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 77 69 74  resentation, wit
0a40: 68 6f 75 74 20 61 6e 79 20 6f 74 68 65 72 20 63  hout any other c
0a50: 6f 6d 70 72 65 73 73 69 6f 6e 0a 0a 23 23 20 4d  ompression..## M
0a60: 6f 74 69 76 61 74 69 6f 6e 0a 0a 54 79 70 69 63  otivation..Typic
0a70: 61 6c 6c 79 20 77 68 65 6e 20 79 6f 75 20 77 61  ally when you wa
0a80: 6e 74 20 74 6f 20 73 68 6f 72 74 65 6e 20 61 20  nt to shorten a 
0a90: 6c 6f 6e 67 20 55 52 4c 20 77 69 74 68 20 6c 61  long URL with la
0aa0: 72 67 65 20 61 6d 6f 75 6e 74 73 20 6f 66 20 64  rge amounts of d
0ab0: 61 74 61 20 70 61 72 61 6d 65 74 65 72 73 2c 20  ata parameters, 
0ac0: 74 68 65 20 61 70 70 72 6f 61 63 68 20 69 73 20  the approach is 
0ad0: 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 22 73  to generate a "s
0ae0: 68 6f 72 74 20 55 52 4c 22 20 77 68 65 72 65 20  hort URL" where 
0af0: 63 6f 6d 70 72 65 73 73 69 6f 6e 20 69 73 20 61  compression is a
0b00: 63 68 69 65 76 65 64 20 62 79 20 75 73 69 6e 67  chieved by using
0b10: 20 61 20 74 68 69 72 64 2d 70 61 72 74 79 20 73   a third-party s
0b20: 65 72 76 69 63 65 20 77 68 69 63 68 20 73 74 6f  ervice which sto
0b30: 72 65 73 20 74 68 65 20 74 72 75 65 20 55 52 4c  res the true URL
0b40: 20 61 6e 64 20 72 65 64 69 72 65 63 74 73 20 74   and redirects t
0b50: 68 65 20 75 73 65 72 20 28 65 2e 67 2e 20 62 69  he user (e.g. bi
0b60: 74 2e 6c 79 20 6f 72 20 67 6f 6f 2e 67 6c 29 2e  t.ly or goo.gl).
0b70: 0a 0a 48 6f 77 65 76 65 72 2c 20 69 66 20 79 6f  ..However, if yo
0b80: 75 20 77 61 6e 74 20 74 6f 3a 0a 0a 2a 20 73 68  u want to:..* sh
0b90: 61 72 65 20 62 6f 6f 6b 6d 61 72 6b 73 20 77 69  are bookmarks wi
0ba0: 74 68 20 76 69 72 74 75 61 6c 6c 79 20 75 6e 6c  th virtually unl
0bb0: 69 6d 69 74 65 64 20 63 6f 6d 62 69 6e 61 74 69  imited combinati
0bc0: 6f 6e 73 20 6f 66 20 73 74 61 74 65 20 61 6e 64  ons of state and
0bd0: 2f 6f 72 0a 2a 20 77 61 6e 74 20 74 6f 20 61 76  /or.* want to av
0be0: 6f 69 64 20 74 68 65 20 74 68 69 72 64 2d 70 61  oid the third-pa
0bf0: 72 74 79 20 64 65 70 65 6e 64 65 6e 63 79 0a 0a  rty dependency..
0c00: 79 6f 75 20 77 6f 75 6c 64 20 65 6e 63 6f 64 65  you would encode
0c10: 20 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74   the data struct
0c20: 75 72 65 20 28 74 79 70 69 63 61 6c 6c 79 20 4a  ure (typically J
0c30: 53 4f 4e 29 20 69 6e 20 79 6f 75 72 20 55 52 4c  SON) in your URL
0c40: 2c 20 62 75 74 20 74 68 69 73 20 6f 66 74 65 6e  , but this often
0c50: 20 72 65 73 75 6c 74 73 20 69 6e 20 76 65 72 79   results in very
0c60: 20 6c 61 72 67 65 20 55 52 4c 73 2e 0a 0a 54 68   large URLs...Th
0c70: 69 73 20 61 70 70 72 6f 61 63 68 20 64 69 66 66  is approach diff
0c80: 65 72 73 20 62 79 20 72 65 6d 6f 76 69 6e 67 20  ers by removing 
0c90: 74 68 61 74 20 74 68 69 72 64 2d 70 61 72 74 79  that third-party
0ca0: 20 64 65 70 65 6e 64 65 6e 63 79 20 61 6e 64 20   dependency and 
0cb0: 65 6e 63 6f 64 65 73 20 69 74 20 75 73 69 6e 67  encodes it using
0cc0: 20 63 6f 6d 6d 6f 6e 20 63 6f 6d 70 72 65 73 73   common compress
0cd0: 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 73  ion algorithms s
0ce0: 75 63 68 20 61 73 20 4c 5a 57 20 6f 72 20 4c 5a  uch as LZW or LZ
0cf0: 4d 41 2e 0a 0a 4e 6f 74 65 3a 20 49 74 20 69 73  MA...Note: It is
0d00: 20 61 72 67 75 61 62 6c 65 20 74 68 61 74 20 61   arguable that a
0d10: 20 63 75 73 74 6f 6d 20 64 69 63 74 69 6f 6e 61   custom dictiona
0d20: 72 79 20 2f 20 64 6f 6d 61 69 6e 20 73 70 65 63  ry / domain spec
0d30: 69 66 69 63 20 65 6e 63 6f 64 69 6e 67 20 77 6f  ific encoding wo
0d40: 75 6c 64 20 75 6c 74 69 6d 61 74 65 6c 79 20 70  uld ultimately p
0d50: 72 6f 76 69 64 65 20 62 65 74 74 65 72 20 63 6f  rovide better co
0d60: 6d 70 72 65 73 73 69 6f 6e 2c 20 62 75 74 20 68  mpression, but h
0d70: 65 72 65 20 77 65 20 77 61 6e 74 20 74 6f 0a 2a  ere we want to.*
0d80: 20 61 76 6f 69 64 20 6d 61 69 6e 74 61 69 6e 69   avoid maintaini
0d90: 6e 67 20 73 75 63 68 20 61 20 64 69 63 74 69 6f  ng such a dictio
0da0: 6e 61 72 79 20 61 6e 64 2f 6f 72 0a 2a 20 72 65  nary and/or.* re
0db0: 74 61 69 6e 20 63 72 6f 73 73 2d 61 70 70 6c 69  tain cross-appli
0dc0: 63 61 74 69 6f 6e 20 63 6f 6d 70 61 74 69 62 69  cation compatibi
0dd0: 6c 69 74 79 20 28 6f 74 68 65 72 77 69 73 65 20  lity (otherwise 
0de0: 79 6f 75 20 6e 65 65 64 20 61 20 73 68 61 72 65  you need a share
0df0: 64 20 64 69 63 74 69 6f 6e 61 72 79 29 0a 0a 23  d dictionary)..#
0e00: 23 20 41 70 70 72 6f 61 63 68 0a 0a 49 20 65 78  # Approach..I ex
0e10: 70 6c 6f 72 65 64 20 73 65 76 65 72 61 6c 20 6f  plored several o
0e20: 70 74 69 6f 6e 73 2c 20 74 68 65 20 6d 6f 73 74  ptions, the most
0e30: 20 70 6f 70 75 6c 61 72 20 6f 6e 65 20 62 65 69   popular one bei
0e40: 6e 67 20 5b 4d 65 73 73 61 67 65 50 61 63 6b 5d  ng [MessagePack]
0e50: 5b 31 5d 2e 20 48 6f 77 65 76 65 72 2c 20 49 20  [1]. However, I 
0e60: 6e 6f 74 69 63 65 64 20 74 68 61 74 20 69 74 20  noticed that it 
0e70: 64 69 64 20 6e 6f 74 20 67 69 76 65 20 74 68 65  did not give the
0e80: 20 62 65 73 74 20 70 6f 73 73 69 62 6c 65 20 63   best possible c
0e90: 6f 6d 70 72 65 73 73 69 6f 6e 20 61 73 20 63 6f  ompression as co
0ea0: 6d 70 61 72 65 64 20 74 6f 20 5b 4c 5a 4d 41 5d  mpared to [LZMA]
0eb0: 5b 32 5d 20 61 6e 64 20 5b 4c 5a 57 5d 5b 33 5d  [2] and [LZW][3]
0ec0: 2e 0a 0a 41 74 20 66 69 72 73 74 20 49 20 74 72  ...At first I tr
0ed0: 69 65 64 20 74 6f 20 61 70 70 6c 79 20 74 68 65  ied to apply the
0ee0: 20 62 69 6e 61 72 79 20 63 6f 6d 70 72 65 73 73   binary compress
0ef0: 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 6f 6e 20  ion directly on 
0f00: 61 20 73 74 72 69 6e 67 69 66 69 65 64 20 4a 53  a stringified JS
0f10: 4f 4e 2c 20 74 68 65 6e 20 49 20 72 65 61 6c 69  ON, then I reali
0f20: 73 65 64 20 74 68 61 74 20 70 61 63 6b 69 6e 67  sed that packing
0f30: 20 69 74 20 66 69 72 73 74 20 72 65 73 75 6c 74   it first result
0f40: 65 64 20 69 6e 20 62 65 74 74 65 72 20 63 6f 6d  ed in better com
0f50: 70 72 65 73 73 69 6f 6e 2e 0a 0a 46 6f 72 20 73  pression...For s
0f60: 6d 61 6c 6c 20 4a 53 20 6f 62 6a 65 63 74 73 2c  mall JS objects,
0f70: 20 4c 5a 57 20 6c 61 72 67 65 6c 79 20 6f 75 74   LZW largely out
0f80: 70 65 72 66 6f 72 6d 65 64 20 4c 5a 4d 41 2c 20  performed LZMA, 
0f90: 62 75 74 20 66 6f 72 20 74 68 65 20 6d 6f 73 74  but for the most
0fa0: 20 70 61 72 74 20 79 6f 75 27 64 20 70 72 6f 62   part you'd prob
0fb0: 61 62 6c 79 20 62 65 20 6c 6f 6f 6b 69 6e 67 20  ably be looking 
0fc0: 74 6f 20 63 6f 6d 70 72 65 73 73 20 6c 61 72 67  to compress larg
0fd0: 65 20 4a 53 4f 4e 20 64 61 74 61 20 72 61 74 68  e JSON data rath
0fe0: 65 72 20 74 68 61 6e 20 73 6d 61 6c 6c 20 61 6d  er than small am
0ff0: 6f 75 6e 74 73 20 28 6f 74 68 65 72 77 69 73 65  ounts (otherwise
1000: 20 61 20 73 69 6d 70 6c 65 20 73 74 72 69 6e 67   a simple string
1010: 69 66 79 20 2b 20 62 61 73 65 36 34 20 69 73 20  ify + base64 is 
1020: 73 75 66 66 69 63 69 65 6e 74 29 2e 20 59 6f 75  sufficient). You
1030: 20 63 61 6e 20 63 68 6f 6f 73 65 20 74 6f 20 75   can choose to u
1040: 73 65 20 77 68 61 74 65 76 65 72 20 63 6f 64 65  se whatever code
1050: 63 20 73 75 69 74 73 20 79 6f 75 20 62 65 73 74  c suits you best
1060: 2e 0a 0a 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20  ...In addition, 
1070: 74 68 65 72 65 20 69 73 20 6e 6f 77 20 73 75 70  there is now sup
1080: 70 6f 72 74 20 66 6f 72 20 5b 4c 5a 53 54 52 49  port for [LZSTRI
1090: 4e 47 5d 5b 35 5d 2c 20 61 6c 74 68 6f 75 67 68  NG][5], although
10a0: 20 74 68 65 20 55 52 49 20 65 6e 63 6f 64 69 6e   the URI encodin
10b0: 67 20 73 74 69 6c 6c 20 75 73 65 73 20 75 72 6c  g still uses url
10c0: 73 61 66 65 2d 62 61 73 65 36 34 20 62 65 63 61  safe-base64 beca
10d0: 75 73 65 20 4c 5a 53 54 52 49 4e 47 20 73 74 69  use LZSTRING sti
10e0: 6c 6c 20 75 73 65 73 20 75 6e 73 61 66 65 20 63  ll uses unsafe c
10f0: 68 61 72 61 63 74 65 72 73 20 76 69 61 20 74 68  haracters via th
1100: 65 69 72 20 60 63 6f 6d 70 72 65 73 73 54 6f 55  eir `compressToU
1110: 52 49 45 6e 63 6f 64 65 64 53 74 72 69 6e 67 60  RIEncodedString`
1120: 20 6d 65 74 68 6f 64 20 2d 20 6e 6f 74 61 62 6c   method - notabl
1130: 79 20 74 68 65 20 5b 60 2b 60 20 63 68 61 72 61  y the [`+` chara
1140: 63 74 65 72 5d 5b 36 5d 0a 0a 46 69 6e 61 6c 6c  cter][6]..Finall
1150: 79 2c 20 49 20 77 65 6e 74 20 77 69 74 68 20 5b  y, I went with [
1160: 75 72 6c 73 61 66 65 2d 62 61 73 65 36 34 5d 5b  urlsafe-base64][
1170: 34 5d 20 74 6f 20 65 6e 63 6f 64 65 20 69 74 20  4] to encode it 
1180: 69 6e 20 61 20 55 52 4c 2d 66 72 69 65 6e 64 6c  in a URL-friendl
1190: 79 20 66 6f 72 6d 61 74 2e 0a 0a 23 23 20 54 4f  y format...## TO
11a0: 44 4f 0a 0a 46 69 6e 64 20 61 20 77 61 79 20 74  DO..Find a way t
11b0: 6f 20 69 6d 70 72 6f 76 65 20 62 75 6e 64 6c 65  o improve bundle
11c0: 20 73 69 7a 65 73 20 66 6f 72 20 62 72 6f 77 73   sizes for brows
11d0: 65 72 20 75 73 61 67 65 2e 0a 0a 5b 31 5d 3a 20  er usage...[1]: 
11e0: 68 74 74 70 3a 2f 2f 6d 73 67 70 61 63 6b 2e 6f  http://msgpack.o
11f0: 72 67 2f 69 6e 64 65 78 2e 68 74 6d 6c 0a 5b 32  rg/index.html.[2
1200: 5d 3a 20 68 74 74 70 73 3a 2f 2f 77 77 77 2e 6e  ]: https://www.n
1210: 70 6d 6a 73 2e 63 6f 6d 2f 70 61 63 6b 61 67 65  pmjs.com/package
1220: 2f 6c 7a 6d 61 0a 5b 33 5d 3a 20 68 74 74 70 73  /lzma.[3]: https
1230: 3a 2f 2f 77 77 77 2e 6e 70 6d 6a 73 2e 63 6f 6d  ://www.npmjs.com
1240: 2f 70 61 63 6b 61 67 65 2f 6e 6f 64 65 2d 6c 7a  /package/node-lz
1250: 77 0a 5b 34 5d 3a 20 68 74 74 70 73 3a 2f 2f 77  w.[4]: https://w
1260: 77 77 2e 6e 70 6d 6a 73 2e 63 6f 6d 2f 70 61 63  ww.npmjs.com/pac
1270: 6b 61 67 65 2f 75 72 6c 73 61 66 65 2d 62 61 73  kage/urlsafe-bas
1280: 65 36 34 0a 5b 35 5d 3a 20 68 74 74 70 3a 2f 2f  e64.[5]: http://
1290: 70 69 65 72 6f 78 79 2e 6e 65 74 2f 62 6c 6f 67  pieroxy.net/blog
12a0: 2f 70 61 67 65 73 2f 6c 7a 2d 73 74 72 69 6e 67  /pages/lz-string
12b0: 2f 69 6e 64 65 78 2e 68 74 6d 6c 0a 5b 36 5d 3a  /index.html.[6]:
12c0: 20 68 74 74 70 73 3a 2f 2f 67 69 74 68 75 62 2e   https://github.
12d0: 63 6f 6d 2f 70 69 65 72 6f 78 79 2f 6c 7a 2d 73  com/pieroxy/lz-s
12e0: 74 72 69 6e 67 2f 62 6c 6f 62 2f 6d 61 73 74 65  tring/blob/maste
12f0: 72 2f 6c 69 62 73 2f 6c 7a 2d 73 74 72 69 6e 67  r/libs/lz-string
1300: 2e 6a 73 23 4c 31 35 0a 0a 5b 64 6f 77 6e 6c 6f  .js#L15..[downlo
1310: 61 64 73 2d 69 6d 61 67 65 5d 3a 20 68 74 74 70  ads-image]: http
1320: 73 3a 2f 2f 69 6d 67 2e 73 68 69 65 6c 64 73 2e  s://img.shields.
1330: 69 6f 2f 6e 70 6d 2f 64 6d 2f 6a 73 6f 6e 2d 75  io/npm/dm/json-u
1340: 72 6c 2e 73 76 67 3f 73 74 79 6c 65 3d 66 6c 61  rl.svg?style=fla
1350: 74 2d 73 71 75 61 72 65 0a 5b 64 6f 77 6e 6c 6f  t-square.[downlo
1360: 61 64 73 2d 75 72 6c 5d 3a 20 68 74 74 70 73 3a  ads-url]: https:
1370: 2f 2f 77 77 77 2e 6e 70 6d 6a 73 2e 63 6f 6d 2f  //www.npmjs.com/
1380: 70 61 63 6b 61 67 65 2f 6a 73 6f 6e 2d 75 72 6c  package/json-url
1390: 0a 5b 74 72 61 76 69 73 2d 69 6d 61 67 65 5d 3a  .[travis-image]:
13a0: 20 68 74 74 70 73 3a 2f 2f 74 72 61 76 69 73 2d   https://travis-
13b0: 63 69 2e 6f 72 67 2f 6d 61 73 6f 74 69 6d 65 2f  ci.org/masotime/
13c0: 6a 73 6f 6e 2d 75 72 6c 2e 73 76 67 3f 62 78 65  json-url.svg?bxe
13d0: 72 61 6e 63 68 3d 6d 61 73 74 65 72 0a 5b 74 72  ranch=master.[tr
13e0: 61 76 69 73 2d 75 72 6c 5d 3a 20 68 74 74 70 73  avis-url]: https
13f0: 3a 2f 2f 74 72 61 76 69 73 2d 63 69 2e 6f 72 67  ://travis-ci.org
1400: 2f 6d 61 73 6f 74 69 6d 65 2f 6a 73 6f 6e 2d 75  /masotime/json-u
1410: 72 6c 0a 5b 64 61 76 69 64 64 6d 2d 69 6d 61 67  rl.[daviddm-imag
1420: 65 5d 3a 20 68 74 74 70 73 3a 2f 2f 64 61 76 69  e]: https://davi
1430: 64 2d 64 6d 2e 6f 72 67 2f 6d 61 73 6f 74 69 6d  d-dm.org/masotim
1440: 65 2f 6a 73 6f 6e 2d 75 72 6c 2e 73 76 67 3f 74  e/json-url.svg?t
1450: 68 65 6d 65 3d 73 68 69 65 6c 64 73 2e 69 6f 0a  heme=shields.io.
1460: 5b 64 61 76 69 64 64 6d 2d 75 72 6c 5d 3a 20 68  [daviddm-url]: h
1470: 74 74 70 73 3a 2f 2f 64 61 76 69 64 2d 64 6d 2e  ttps://david-dm.
1480: 6f 72 67 2f 6d 61 73 6f 74 69 6d 65 2f 6a 73 6f  org/masotime/jso
1490: 6e 2d 75 72 6c 0a 5b 63 6f 76 65 72 61 67 65 2d  n-url.[coverage-
14a0: 69 6d 61 67 65 5d 3a 20 68 74 74 70 73 3a 2f 2f  image]: https://
14b0: 63 6f 76 65 72 61 6c 6c 73 2e 69 6f 2f 72 65 70  coveralls.io/rep
14c0: 6f 73 2f 67 69 74 68 75 62 2f 6d 61 73 6f 74 69  os/github/masoti
14d0: 6d 65 2f 6a 73 6f 6e 2d 75 72 6c 2f 62 61 64 67  me/json-url/badg
14e0: 65 2e 73 76 67 3f 62 72 61 6e 63 68 3d 6d 61 73  e.svg?branch=mas
14f0: 74 65 72 0a 5b 63 6f 76 65 72 61 67 65 2d 75 72  ter.[coverage-ur
1500: 6c 5d 3a 20 68 74 74 70 73 3a 2f 2f 63 6f 76 65  l]: https://cove
1510: 72 61 6c 6c 73 2e 69 6f 2f 67 69 74 68 75 62 2f  ralls.io/github/
1520: 6d 61 73 6f 74 69 6d 65 2f 6a 73 6f 6e 2d 75 72  masotime/json-ur
1530: 6c 3f 62 72 61 6e 63 68 3d 6d 61 73 74 65 72 0a  l?branch=master.