npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

utf8js

v0.1.4

Published

Fast UTF-8 encoding/decoding for browsers and Node.

Downloads

65

Readme

utf8js NPM version Build Status codecov

utf8.js is a fast UTF-8 encoder/decoder for JavaScript. It attempts to use native encoding/decoding methods whenever possible. It requires Promises and Typed Arrays, both of which can be polyfilled. On modern computers, when using native methods, the decoding speeds should exceed 100 MiB/s. Encoding should be fast even if native methods are not available.

API

  • encode(string: DOMString): Promise<ArrayBuffer>
  • decode(buffer: ArrayBuffer): Promise<DOMString>

If you can't use asynchronous methods for a given task, you may be able to hack around it. Only the Blob codec is truly asynchronous - the rest return an already-resolved Promise.

What, why, how?

To be specific, utf8.js deals with the conversion between UTF-16 DOMStrings and UTF-8 TypedArrays. There are plenty of existing UNICODE libraries on npm, why write a new one? Well, many existing libraries do not handle large amounts of data well. When dealing with strings or arrays of over 100 MiB of data, transforming the string multiple times is not wise. In fact, it is surprisingly difficult for JavaScript code to construct large strings in the first place, without running out of memory.

That is why this library has numerous implementations, for various cases:

  • NativeCodec uses the new TextEncoder/TextDecoder API and achieves over 300MiB/s on my computer.
  • BlobCodec abuses Blob objects to go to and from UTF-8 and achieves over 200MiB/s on my computer (even on Internet Explorer 10!)
  • NodeJSCodec uses the Node.js-specific Buffer API.
  • JSCodec is a pure JS codec. Although encoding is quick, decoding is both slow and memory intensive.

For almost any browser IE 10+, native methods will work.

Why not just polyfill TextEncoder/TextDecoder?

For similar reasons! A great polyfill exists, but it doesn't support fallback to Blob, a very useful thing to have on Internet Explorer 10. I haven't tested but I imagine its pure JS UTF-8 encoding/decoding is probably also not fit for dealing with large amounts of data.

Browser support

Approximate browser support:

  • Chrome: 32+
  • Firefox: 29+
  • Internet Explorer: 10+
  • Safari: 7.1+

Chrome 7+ and Firefox 4+ should also work when a Promise polyfill is present. Internet Explorer requires a Promise polyfill, since it still doesn't support Promises. Earlier versions of IE will probably work when provided a TypedArray polyfill - at least IE 9 - but do not expect stellar performance or memory usage when dealing with huge strings.

TODO

  • Testing needed! It would be nice to automate tests across other browsers, but that might be a long way off.
  • Explore options for faster encoding/decoding in IE 9. Perhaps a Microsoft-specific object can be abused to provide efficient decoding?
  • Can DOMStrings be abused to store binary data and UTF-8 data when TypedArray is not available? Sure looks like it, but it's obviously very inefficient to mutate.