Skip to content

Fast polyfill for TextEncoder and TextDecoder, only supports UTF-8

License

Notifications You must be signed in to change notification settings

tetherto/fast-text-encoding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ea6efc7 · Aug 30, 2022

History

65 Commits
Jun 20, 2022
Apr 28, 2020
Aug 30, 2022
Aug 29, 2022
Jun 20, 2022
Aug 26, 2022
Sep 1, 2017
Aug 30, 2022
Aug 29, 2022
Aug 30, 2022
Aug 30, 2022
Jun 20, 2022
Mar 6, 2020
Aug 29, 2022
Aug 29, 2022
Aug 29, 2022
Aug 29, 2022

Repository files navigation

Test

This is a fast polyfill for TextEncoder and TextDecoder, which let you encode and decode JavaScript strings into UTF-8 bytes.

It is fast partially as it does not support^ any encodings aside UTF-8 (and note that natively, only TextDecoder supports alternative encodings anyway). See some benchmarks.

^If this polyfill used on Node v5.1 through v11 (when Text... was introduced), then this simply wraps Buffer, which supports many encodings and is native code.

Usage

Install as "fast-text-encoding" via your favourite package manager.

You only need this polyfill if you're supporting older browsers like IE, legacy Edge, ancient Chrome and Firefox, or Node before v11.

Browser

Include the minified code inside a script tag or as an ES6 Module for its side effects. It will create TextEncoder and TextDecoder if the symbols are missing on window or global.

<script src="node_modules/fast-text-encoding/text.min.js"></script>
<script type="module">
  import './node_modules/fast-text-encoding/text.min.js';
  import 'fast-text-encoding';  // or perhaps this
  // confidently do something with TextEncoder or TextDecoder \o/
</script>

⚠️ You'll probably want to depend on text.min.js, as it's compiled to ES5 for older environments.

Node

You only need this polyfill in Node before v11. However, you can use Buffer to provide the same functionality (but not conforming to any spec) in versions even older than that.

require('fast-text-encoding');  // just require me before use

const buffer = new TextEncoder().encode('Turn me into UTF-8!');
// buffer is now a Uint8Array of [84, 117, 114, 110, ...]

In Node v5.1 and above, this polyfill uses Buffer to implement TextDecoder.

Release

Compile code with Closure Compiler.

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name text.min.js
// ==/ClosureCompiler==

// code here

About

Fast polyfill for TextEncoder and TextDecoder, only supports UTF-8

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 97.0%
  • HTML 2.3%
  • Shell 0.7%