Socket
Socket
Sign inDemoInstall

fastestsmallesttextencoderdecoder

Package Overview
Dependencies
0
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fastestsmallesttextencoderdecoder

The fastest smallest Javascript polyfill for the encode of TextEncoder and decode of TextDecoder for UTF-8 only. Made by AnonyCo with โค๏ธ from ๐Ÿ•s.


Version published
Weekly downloads
161K
decreased by-8.12%
Maintainers
1
Install size
43.5 kB
Created
Weekly downloads
ย 

Readme

Source

npm version GitHub stars GitHub file size in bytes GitHub file size in bytes npm bundle size (version) npm downloads CC0 license

This Javascript library provides the most performant tiny polyfill for window.TextEncoder, TextEncoder.prototype.encodeInto, and window.TextDecoder for use in the browser, in NodeJS, in RequireJS, in web Workers, in SharedWorkers, and in ServiceWorkers.

Quick Start

Add the following HTML Code inside the <head>:

<script src="https://dl.dropboxusercontent.com/s/r55397ld512etib/EncoderDecoderTogether.min.js?dl=0" nomodule="" type="text/javascript"></script>

If no script on the page requires this library until the DOMContentLoaded event, then use the the much less blocking version below:

<script defer="" src="https://dl.dropboxusercontent.com/s/r55397ld512etib/EncoderDecoderTogether.min.js?dl=0" nomodule="" type="text/javascript"></script>

Alternatively, either use https://dl.dropboxusercontent.com/s/47481btie8pb95h/FastestTextEncoderPolyfill.min.js?dl=0 to polyfill window.TextEncoder for converting a String into a Uint8Array or use https://dl.dropboxusercontent.com/s/qmoknmp86sytc74/FastestTextDecoderPolyfill.min.js?dl=0 to only polyfill window.TextDecoder for converting a Uint8Array/ArrayBuffer/[typedarray]/global.Buffer into a String.

The nomodule attribute prevents the script from being needlessly downloaded and executed on browsers which already support TextEncoder and TextDecoder. nomodule does not test for the presence of TextEncoder or TextDecoder, but it is very safe to assume that browsers advanced enough to support modules also support TextEncoder and TextDecoder.

EncodeInto

See the MDN here for documentation. For the TextEncoder.prototype.encodeInto polyfill, please use https://dl.dropboxusercontent.com/s/i2e2rho1ohtbhfg/EncoderDecoderTogether.min.js?dl=0 for the full package, https://dl.dropboxusercontent.com/s/nlcgzbr0ayd5pjs/FastestTextEncoderPolyfill.min.js?dl=0 for only TextEncoder and TextEncoder.prototype.encodeInto, and npm i fastestsmallesttextencoderdecoder-encodeinto for NodeJS, es6 modules, RequireJS, AngularJS, or whatever it is that floats your boat. The encodeInto folder of this repository contains the auto-generated encodeInto build of the main project. The npm project is fastestsmallesttextencoderdecoder-encodeinto:

npm install fastestsmallesttextencoderdecoder-encodeinto

RequireJS and NodeJS

For dropping into either RequireJS or NodeJS, please use the fastestsmallesttextencoderdecoder npm repository, this minified file, or the corresponding source code file. To install via npm, use the following code.

npm install fastestsmallesttextencoderdecoder

Alternatively, if one do not know how to use the command line, save the script corresponding to one's operating system to the directory where the nodejs script will run and use the file manager to run the script (on Windows, it's a double-click).

After installing via npm, one can use require("fastestsmallesttextencoderdecoder"). Alternatively, one can drop the EncoderAndDecoderNodeJS.min.js file into the same directory as their NodeJS script and do require("./EncoderAndDecoderNodeJS.min.js"). Both methods are functionally equivalent.

AngularJS

Open a terminal in the project's directory, and install fastestsmallesttextencoderdecoder via npm.

npm install fastestsmallesttextencoderdecoder

Then, add import 'fastestsmallesttextencoderdecoder'; to the polyfills.ts file.

Benchmarks

Don't take my word that FastestSmallestTextEncoderDecoder is the fastest. Instead, check out the benchmarks below. You can run your own benchmarks by cloning this repo and running npm run benchmark, but beware that you need a beefy computer with plenty of free RAM, as the NodeJS garbage collector is disabled via --noconcurrent_sweeping --nouse-idle-notification so that it does not interfer with the timing of the tests (the GC is runned manually via global.gc(true) at the conclusion of the tests).

The tests below were performed on an ascii file. To ensure consistancy, all test results are the mean of the IQR of many many trials. The checkmark "โœ”" means that the encoder/decoder implementation gave the correct output, whereas a bold "โœ—" indicates an incorrect output. This extra check is signifigant because relying on a faulty encoder/decoder can lead to inconsistant behaviors in code that defaults to using the native implementation where available.

LibraryDecode 32 bytesDecode 32768Decode 16777216Encode 32 bytesEncode 32768Encode 16777216
Native10201 KB/sec โœ”806451 KB/sec โœ”907381 KB/sec โœ”53415 KB/sec โœ”4661211 KB/sec โœ”1150916 KB/sec โœ”
FastestSmallestTextEncoderDecoder18038 KB/sec โœ”154839 KB/sec โœ”168984 KB/sec โœ”21667 KB/sec โœ”404279 KB/sec โœ”681429 KB/sec โœ”
fast-text-encoding17518 KB/sec โœ”71806 KB/sec โœ”99017 KB/sec โœ”22713 KB/sec โœ”240880 KB/sec โœ”445137 KB/sec โœ”
text-encoding-shim10205 KB/sec โœ”17503 KB/sec โœ”27971 KB/sec โœ”14044 KB/sec โœ”50007 KB/sec โœ”88687 KB/sec โœ”
TextEncoderLite12433 KB/sec โœ”23456 KB/sec โœ”13929 KB/sec โœ”24013 KB/sec โœ”57034 KB/sec โœ”62119 KB/sec โœ”
TextEncoderTextDecoder.js4469 KB/sec โœ”5956 KB/sec โœ”5626 KB/sec โœ”13576 KB/sec โœ”37667 KB/sec โœ”57916 KB/sec โœ”
text-encoding3084 KB/sec โœ”6762 KB/sec โœ”7925 KB/sec โœ”8621 KB/sec โœ”26699 KB/sec โœ”35755 KB/sec โœ”

Needless to say, FastestSmallestTextEncoderDecoder outperformed every other polyfill out there. Infact, it is so fast that it outperformed the native implementation on a set of 32 ascii bytes. The tests below were performed on a mixed ascii-utf8 file.

LibraryDecode 32 bytesDecode 32768Decode 16777216Encode 32 bytesEncode 32768Encode 16777216
Native24140 KB/sec โœ”365043 KB/sec โœ”512133 KB/sec โœ”54183 KB/sec โœ”293455 KB/sec โœ”535203 KB/sec โœ”
FastestSmallestTextEncoderDecoder13932 KB/sec โœ”113823 KB/sec โœ”141706 KB/sec โœ”20755 KB/sec โœ”212100 KB/sec โœ”443344 KB/sec โœ”
fast-text-encoding10738 KB/sec โœ”62851 KB/sec โœ”94031 KB/sec โœ”15105 KB/sec โœ”104843 KB/sec โœ”320778 KB/sec โœ”
TextEncoderLite6594 KB/sec โœ”9893 KB/sec โœ”10470 KB/sec โœ”17660 KB/sec โœ—53905 KB/sec โœ—57862 KB/sec โœ—
text-encoding-shim10778 KB/sec โœ”15063 KB/sec โœ”24373 KB/sec โœ”27296 KB/sec โœ”31496 KB/sec โœ”42497 KB/sec โœ”
TextEncoderTextDecoder.js5558 KB/sec โœ”5121 KB/sec โœ”6580 KB/sec โœ”14583 KB/sec โœ”32261 KB/sec โœ”60183 KB/sec โœ”
text-encoding3531 KB/sec โœ”6669 KB/sec โœ”7983 KB/sec โœ”7233 KB/sec โœ”20343 KB/sec โœ”29136 KB/sec โœ”

FastestSmallestTextEncoderDecoder excells at encoding lots of complex unicode and runs at 83% the speed of the native implementation. In the next test, let's examine a more real world exampleโ€”the 1876 The Russian Synodal Bible.txt. It's a whoping 4.4MB rat's-nest of complex Russian UTF-8, sure to give any encoder/decoder a bad day. Let's see how they perform at their worst.

LibraryDecode Russian BibleEncode Russian Bible
Native626273 KB/sec โœ”951538 KB/sec โœ”
FastestSmallestTextEncoderDecoder228360 KB/sec โœ”428625 KB/sec โœ”
fast-text-encoding94666 KB/sec โœ”289109 KB/sec โœ”
text-encoding-shim29335 KB/sec โœ”60508 KB/sec โœ”
TextEncoderLite14079 KB/sec โœ”61648 KB/sec โœ”
TextEncoderTextDecoder.js5989 KB/sec โœ”54741 KB/sec โœ”
text-encoding7919 KB/sec โœ”28043 KB/sec โœ”

Browser Support

This polyfill will bring support for TextEncoder/TextDecoder to the following browsers.

FeatureChrome Firefox Opera Edge Internet Explorer Safari Android Samsung Internet Node.js
Full Polyfill7.04.011.612.0**105.1 (Desktop) / 4.2 (iOS)4.01.03.0
Partial Polyfill*1.0**0.67.0 (Desktop) / 9.5** (Mobile)12.0**4.02.01.0**1.0**0.10

Also note that while this polyfill may work in these old browsers, it is very likely that the rest of one's website will not work unless if one makes a concious effort to have their code work in these old browsers.

* Partial polyfill means that Array (or Buffer in NodeJS) will be used instead of Uint8Array/[typedarray].

** This is the first public release of the browser

API Documentation

Please review the MDN at window.TextEncoder and window.TextDecoder for information on how to use TextEncoder and TextDecoder.

As for NodeJS, calling require("EncoderAndDecoderNodeJS.min.js") yields the following object. Note that this polyfill checks for global.TextEncoder and global.TextDecoder and returns the native implementation if available.

module.exports = {
	TextEncoder: function TextEncoder(){/*...*/},
	TextDecoder: function TextDecoder(){/*...*/},
	encode: TextEncoder.prototype.encode,
	decode: TextDecoder.prototype.decode
}

In NodeJS, one does not ever have to use new just to get the encoder/decoder (although one still can do so if they want to). All of the code snippets below function identically (aside from unused local variables introduced into the scope).

    // Variation 1
    const {TextEncoder, TextDecoder} = require("fastestsmallesttextencoderdecoder");
    const encode = (new TextEncoder).encode;
    const decode = (new TextDecoder).decode;
    // Variation 2
    const {encode, decode} = require("fastestsmallesttextencoderdecoder");
    // Variation 3 (a rewording of Variation 2)
    const encodeAndDecodeModule = require("fastestsmallesttextencoderdecoder");
    const encode = encodeAndDecodeModule.encode;
    const decode = encodeAndDecodeModule.decode;

Or, one can use the new and shiny ES6 module importation statements.

    // Variation 1
    import {TextEncoder, TextDecoder} from "fastestsmallesttextencoderdecoder";
    const encode = (new TextEncoder).encode;
    const decode = (new TextDecoder).decode;
    // Variation 2
    import {encode, decode} from "fastestsmallesttextencoderdecoder";
    // Variation 3 (a rewording of Variation 2)
    import * as encodeAndDecodeModule from "fastestsmallesttextencoderdecoder";
    const encode = encodeAndDecodeModule.encode;
    const decode = encodeAndDecodeModule.decode;

Demonstration

Visit the GithubPage to see a demonstation. As seen in the Web Worker hexWorker.js, the Github Pages demonstration uses a special encoderAndDecoderForced.src.js version of this library to forcefully install the TextEncoder and TextDecoder even when there is native support. That way, this demonstraton should serve to truthfully demonstrate this polyfill.

npm Project

This project can be found on npm here at this link.

Development

On Linux, the project can be developed by cloning it with the following command line. The development scripts are designed to be interpeted by Dash, and whether they work on Mac OS is unknown, but they certainly won't work on Windows.

git clone https://github.com/anonyco/FastestSmallestTextEncoderDecoder.git; cd FastestSmallestTextEncoderDecoder; npm run install-dev

Emphasize the npm run install-dev, which downloads closure-compiler.jar into the repository for minifying the files.

Now that the repository is cloned, edit the files as one see fit. Do not edit the files in the encodeInto folder. Those are all auto-generated by having Closure Compiler set ENCODEINTO_BUILD to true and removing dead code for compactness. Also, do not run npm run build in the encodeInto. That's done automatically when npm run build is runned in the topmost folder. Now that the files have been edited, run the following in the terminal in the root folder of the repository in order to minify the NodeJS JavaScript files.

npm run build

To edit tests, edit test/node.js. These tests are compared against the native implementation to ensure validity. To run tests, do the following.

npm run test

Continuity

Feel free to reach out to me at wowzeryest@gmail.com. I am fairly attentive to my github account, but in the unlikely event that issues/pulls start piling up, I of course welcome others to step in and contribute. I am widely open to input and collaboration from anyone on all of my projects.

Keywords

FAQs

Last updated on 09 Jun 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc