Giter VIP home page Giter VIP logo

guybedford / fastestsmallesttextencoderdecoder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anonyco/fastestsmallesttextencoderdecoder

1.0 2.0 0.0 127 KB

The fastest smallest Javascript polyfill for the encode of TextEncoder and decode of TextDecoder for UTF-8 only.

Home Page: https://anonyco.github.io/FastestSmallestTextEncoderDecoder/gh-pages/

License: The Unlicense

JavaScript 74.98% HTML 25.02%

fastestsmallesttextencoderdecoder's Introduction

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

This Javascript library provides the most performant tiny polyfill for window.TextEncoder 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 to your head:

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

If you know that no script on the page requires this library until the DOMContentLoaded event, then you can switch to the much less blocking version below:

<script defer="" src="https://dl.dropboxusercontent.com/s/r55397ld512etib/EncoderDecoderTogether.min.js?dl=0" 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.

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.

Browser Support

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

Feature Chrome Firefox Opera Edge Internet Explorer Safari Android Samsung Internet Node.js
Full Polyfill 7.0 4.0 11.6 12.0** 10 5.1 (Desktop) / 4.2 (iOS) 4.0 1.0 3.0
Partial Polyfill* 1.0 0.6 7.0 (Desktop) / 9.5** (Mobile) 12.0** 4.0 2.0 1.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 your website will not (unless if you make a concious effort about it which I would not reccomend because noone uses or should use these old browsers).

* Partial polyfill means that Array (or ArrayBuffer 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:

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

Thus, in NodeJS, you do not ever have to use new just to get the encoder/decoder (although you still can do so if you want to). All of the code snippets below function identically (aside from unused local variables introduced into the scope). There are an innumerable number of ways to rewrite this same snippet of code, thus I only chose the three which I thought would be most useful.

    // Variation 1
    const {TextEncoder, TextDecoder} = require("EncoderAndDecoderNodeJS.min.js");
    const encode = (new TextEncoder).encode;
    const decode = (new TextDecoder).decode;
    // Variation 2
    const {encode, decode} = require("EncoderAndDecoderNodeJS.min.js");
    // Variation 3
    const encodeAndDecodeModule = require("EncoderAndDecoderNodeJS.min.js");
    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

You can find this project on npm here at this link.

fastestsmallesttextencoderdecoder's People

Contributors

anonyco avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.