Skip to content

Instantly share code, notes, and snippets.

@SaltwaterC
Created August 8, 2012 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaltwaterC/3294812 to your computer and use it in GitHub Desktop.
Save SaltwaterC/3294812 to your computer and use it in GitHub Desktop.
JavaScript string encoding
> Buffer.byteLength('\uD834')
3
> Buffer.byteLength('\uDF06')
3
> Buffer.byteLength('\uD834 \uDF06')
7
// as surrogate pair
> Buffer.byteLength('\uD834\uDF06')
4

> Buffer.byteLength('\uD834', 'ucs2')
2
> Buffer.byteLength('\uDF06', 'ucs2')
2
// this one uses the space char, \u0020 in a surrogate pair
> Buffer.byteLength('\uD834 \uDF06', 'ucs2')
6
> Buffer.byteLength('\uD834\u0020\uDF06', 'ucs2')
6
// as surrogate pair
> Buffer.byteLength('\uD834\uDF06', 'ucs2')
4

> Buffer.byteLength('𝌆')
4
> Buffer.byteLength('𝌆', 'ucs2')
4

// this one has different code points, based on encoding
// \u20AC may be used instead of €
> Buffer.byteLength('€')
3
> Buffer.byteLength('€', 'ucs2')
2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment