Coder Social home page Coder Social logo

anonyco / fastestsmallesttextencoderdecoder Goto Github PK

View Code? Open in Web Editor NEW
127.0 2.0 29.0 57.71 MB

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

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

License: Creative Commons Zero v1.0 Universal

JavaScript 94.47% HTML 5.38% Batchfile 0.07% Shell 0.08%
utf-8 encoder decoder javascript polyfill js utf8 compact cross-browser utf8-string

fastestsmallesttextencoderdecoder's Introduction

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.

Library Decode 32 bytes Decode 32768 Decode 16777216 Encode 32 bytes Encode 32768 Encode 16777216
Native 10201 KB/sec ✔ 806451 KB/sec ✔ 907381 KB/sec ✔ 53415 KB/sec ✔ 4661211 KB/sec ✔ 1150916 KB/sec ✔
FastestSmallestTextEncoderDecoder 18038 KB/sec ✔ 154839 KB/sec ✔ 168984 KB/sec ✔ 21667 KB/sec ✔ 404279 KB/sec ✔ 681429 KB/sec ✔
fast-text-encoding 17518 KB/sec ✔ 71806 KB/sec ✔ 99017 KB/sec ✔ 22713 KB/sec ✔ 240880 KB/sec ✔ 445137 KB/sec ✔
text-encoding-shim 10205 KB/sec ✔ 17503 KB/sec ✔ 27971 KB/sec ✔ 14044 KB/sec ✔ 50007 KB/sec ✔ 88687 KB/sec ✔
TextEncoderLite 12433 KB/sec ✔ 23456 KB/sec ✔ 13929 KB/sec ✔ 24013 KB/sec ✔ 57034 KB/sec ✔ 62119 KB/sec ✔
TextEncoderTextDecoder.js 4469 KB/sec ✔ 5956 KB/sec ✔ 5626 KB/sec ✔ 13576 KB/sec ✔ 37667 KB/sec ✔ 57916 KB/sec ✔
text-encoding 3084 KB/sec ✔ 6762 KB/sec ✔ 7925 KB/sec ✔ 8621 KB/sec ✔ 26699 KB/sec ✔ 35755 KB/sec ✔

Needless to say, FastestSmallestTextEncoderDecoder outperformed almost every other polyfill out there, with the only exception being fast-text-encoding outperforming fastestsmallesttextencoderdecoder on encoding extremely tiny strings. 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.

Library Decode 32 bytes Decode 32768 Decode 16777216 Encode 32 bytes Encode 32768 Encode 16777216
Native 24140 KB/sec ✔ 365043 KB/sec ✔ 512133 KB/sec ✔ 54183 KB/sec ✔ 293455 KB/sec ✔ 535203 KB/sec ✔
FastestSmallestTextEncoderDecoder 13932 KB/sec ✔ 113823 KB/sec ✔ 141706 KB/sec ✔ 20755 KB/sec ✔ 212100 KB/sec ✔ 443344 KB/sec ✔
fast-text-encoding 10738 KB/sec ✔ 62851 KB/sec ✔ 94031 KB/sec ✔ 15105 KB/sec ✔ 104843 KB/sec ✔ 320778 KB/sec ✔
TextEncoderLite 6594 KB/sec ✔ 9893 KB/sec ✔ 10470 KB/sec ✔ 17660 KB/sec 53905 KB/sec 57862 KB/sec
text-encoding-shim 10778 KB/sec ✔ 15063 KB/sec ✔ 24373 KB/sec ✔ 27296 KB/sec ✔ 31496 KB/sec ✔ 42497 KB/sec ✔
TextEncoderTextDecoder.js 5558 KB/sec ✔ 5121 KB/sec ✔ 6580 KB/sec ✔ 14583 KB/sec ✔ 32261 KB/sec ✔ 60183 KB/sec ✔
text-encoding 3531 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.

Library Decode Russian Bible Encode Russian Bible
Native 626273 KB/sec ✔ 951538 KB/sec ✔
FastestSmallestTextEncoderDecoder 228360 KB/sec ✔ 428625 KB/sec ✔
fast-text-encoding 94666 KB/sec ✔ 289109 KB/sec ✔
text-encoding-shim 29335 KB/sec ✔ 60508 KB/sec ✔
TextEncoderLite 14079 KB/sec ✔ 61648 KB/sec ✔
TextEncoderTextDecoder.js 5989 KB/sec ✔ 54741 KB/sec ✔
text-encoding 7919 KB/sec ✔ 28043 KB/sec ✔

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 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 [email protected]. 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.

fastestsmallesttextencoderdecoder's People

Contributors

anonyco avatar guybedford avatar keithamus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fastestsmallesttextencoderdecoder's Issues

Caveat for angular projects

We are using this library as a polyfill for IE11 which appears to not have TextEncoder defined.
We are using Angular 8.

I previously had version 1.0.8 installed, and in my polyfills.ts I had import 'fastestsmallesttextencoderdecoder'; and all was well in Chrome and Firefox, but IE11 had a problem with that, with TextEncoder being undefined.

After updating to 1.0.14 and making no other changes, I was getting errors from the library that f.decode is undefined. I think it had to do with the version of the library that was being loaded in the browser, it appeared to either be incompatible with Chrome/Firefox or the build process was using the wrong file (perhaps it was using the node file in the browser?).

After changing my import to specifically target the browser version, i.e. import 'fastestsmallesttextencoderdecoder/EncoderDecoderTogether.min'; in my polyfills.ts, all is well AND IE11 is now working just fine.

TextEncoder is not a constructor

In version 1.0.4 calling the text encoder like

new TextEncoder('utf-8').encode(value).length

causes the following error
TypeError: fastestsmallesttextencoderdecoder__WEBPACK_IMPORTED_MODULE_0__.TextEncoder is not a constructor

This error does not occur in v1.0.3

Subslice decodes entire array in IE11

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <script src="https://dl.dropboxusercontent.com/s/r55397ld512etib/EncoderDecoderTogether.min.js?dl=0" nomodule="" type="text/javascript"></script>
    <script type="text/javascript">
        const bytes = [50,65,113,117,121,81,111,98,118,68,76,43,77,110,73,90,49,100,43,77,65,71,119,87,68,82,115,57,74,54,117,97,79,78,120,74,119,54,88,113,120,86,99];
        var allBytes = new Uint8Array(16777216);
        // write some A's to the beginning
        for (var i = 100 - 1; i >= 0; i--) {
            allBytes[i] = 65;
        }

        const offset = 242839;
        for (var i = bytes.length - 1; i >= 0; i--) {
            allBytes[i + offset] = bytes[i];
        }
        const slice = allBytes.subarray(offset, offset + bytes.length);
        console.log("slice", slice.length, slice[0], slice);
        const str = new TextDecoder().decode(slice);
        console.log("str", str.length, str);
    </script>
</body>
</html>

Output on IE11:

slice 43 50 [object Uint8Array]
str 16777216 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Output in Firefox 80 (expected output):

slice 43 50 Uint8Array(43) [ 50, 65, 113, 117, 121, 81, 111, 98, 118, 68, … ]
str 43 2AquyQobvDL+MnIZ1d+MAGwWDRs9J6uaONxJw6XqxVc

As you can see, on IE11, the slice returned from subarray has the correct length and correct first byte, but somehow decode decodes the entire allBytes array (until it finds a 0 byte?).

TypeError: Cannot read property 'allocUnsafe' of undefined

I'm executing this polyfill in v8, and getting:

TypeError: Cannot read property 'allocUnsafe' of undefined

It's coming from this line:

var NativeBuffer_allocUnsafe = NativeBuffer["allocUnsafe"];

Commenting out that line appears to fix the problem. Perhaps setting up NativeBuffer_allocUnsafe should be guarded by some other conditions? (My javascript is not strong.)

Does not work in Node

I used nvm to verify it works on 0.10 and 0.12, but then breaks on node 4 or higher. https://nodejs.org/en/download/releases/

asa:~/repos$ cat test.js 

var tmp = require('fastestsmallesttextencoderdecoder')

console.log(
  tmp.decode(tmp.encode("Hello"))
)
asa:~/repos$ nvm install 0.12; nvm use 0.12; node --version; node test.js
Downloading and installing node v0.12.18...
Downloading https://nodejs.org/dist/v0.12.18/node-v0.12.18-linux-x64.tar.xz...
########################################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v0.12.18 (npm v2.15.11)
Now using node v0.12.18 (npm v2.15.11)
v0.12.18
Hello
asa:~/repos$ nvm install 4; nvm use 4; node --version; node test.js
v4.9.1 is already installed.
Now using node v4.9.1 (npm v2.15.11)
Now using node v4.9.1 (npm v2.15.11)
v4.9.1
/home/asa/repos/node_modules/fastestsmallesttextencoderdecoder/NodeJS/EncoderAndDecoderNodeJS.min.js:2
A);for(var a=b.length|0,c=new (h?k:e)(a),d=0;d<a;d=d+1|0)c[d]=b.charCodeAt(d)|0;return c},q=function(){},A=function(b){var a=b.charCodeAt(0)|0;if(55296<=a&&56319>=a){var c=b.charCodeAt(1)|0;if(c===c&&56320<=c&&57343>=c){if(a=(a-55296<<10)+c-56320+65536|0,65535<a)return g(240|a>>>18,128|a>>>12&63,128|a>>>6&63,128|a&63)}else return g(239,191,189)}return 127>=a?b:2047>=a?g(192|a>>>6,128|a&63):g(224|a>>>12,128|a>>>6&63,128|a&63)},r=function(b){b=b&&b.buffer||b;var a=m.call(b);if(a!==z&&a!==y)throw Error("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
                                                                                                                                                                                                                                                                                                        

Error: Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'
asa:~/repos$ nvm install 13; nvm use 13; node --version; node test.js
v13.5.0 is already installed.
Now using node v13.5.0 (npm v6.13.4)
Now using node v13.5.0 (npm v6.13.4)
v13.5.0
/home/asa/repos/node_modules/fastestsmallesttextencoderdecoder/NodeJS/EncoderAndDecoderNodeJS.min.js:2
A);for(var a=b.length|0,c=new (h?k:e)(a),d=0;d<a;d=d+1|0)c[d]=b.charCodeAt(d)|0;return c},q=function(){},A=function(b){var a=b.charCodeAt(0)|0;if(55296<=a&&56319>=a){var c=b.charCodeAt(1)|0;if(c===c&&56320<=c&&57343>=c){if(a=(a-55296<<10)+c-56320+65536|0,65535<a)return g(240|a>>>18,128|a>>>12&63,128|a>>>6&63,128|a&63)}else return g(239,191,189)}return 127>=a?b:2047>=a?g(192|a>>>6,128|a&63):g(224|a>>>12,128|a>>>6&63,128|a&63)},r=function(b){b=b&&b.buffer||b;var a=m.call(b);if(a!==z&&a!==y)throw Error("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
                                                                                                                                                                                                                                                                                                        

Error: Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'

Broken source map reference?

Here is the warning:

WARNING in /app/node_modules/fastestsmallesttextencoderdecoder/EncoderDecoderTogether.min.js
Module Warning (from /app/node_modules/source-map-loader/index.js):
(Emitted value instead of an instance of Error) Cannot find SourceMap 'https://cdn.jsdelivr.net/gh/AnonyCo/FastestSmallestTextEncoderDecoder/EncoderDecoderTogether.min.js.map': Error: Can't resolve './https://cdn.jsdelivr.net/gh/AnonyCo/FastestSmallestTextEncoderDecoder/EncoderDecoderTogether.min.js.map' in '/app/node_modules/fastestsmallesttextencoderdecoder'
....
@ ./src/main.ts

Note that the url starts with './https://cdn...'

Throwing an error if argument of decode is undefined

I think this polyfill behavior is different from the native version for the function decode of TextDecoder.
When using new TextDecoder.decode(); with no argument :
This polyfill will throw a blocking error, whereas using the native one returns an empty string.

encodeInto Implementation does not pass web platform tests for encodeInto

We noticed encoding issues using this library in production, and after some investigation it turns out there's a correctness issue in the library. I took the official WPT testcases and threw a loose harness around them to make them runnable:

// META: global=window,worker
// META: script=/common/sab.js
delete TextEncoder;
require('fastestsmallesttextencoderdecoder-encodeinto/EncoderDecoderTogether.min');
self = globalThis;

function createBuffer(t, s) {
  return new ArrayBuffer(s);
}

function assert_equals(a, b) {
  if (a !== b) {
    throw new Error(`Assertion failed: ${a} is not equal to ${b}`);
  }
}

function assert_throws_js(e, c) {
  try {
    c();
  } catch (err) {
    if (!(err instanceof e)) {
      throw err;
    }
  }
}

function test(t, d) {
  try {
    t();
  } catch (e) {
    console.log('Test failed with error', e);
  }
}

[
  {
    input: 'Hi',
    read: 0,
    destinationLength: 0,
    written: [],
  },
  {
    input: 'A',
    read: 1,
    destinationLength: 10,
    written: [0x41],
  },
  {
    input: '\u{1D306}', // "\uD834\uDF06"
    read: 2,
    destinationLength: 4,
    written: [0xf0, 0x9d, 0x8c, 0x86],
  },
  {
    input: '\u{1D306}A',
    read: 0,
    destinationLength: 3,
    written: [],
  },
  {
    input: '\uD834A\uDF06A¥Hi',
    read: 5,
    destinationLength: 10,
    written: [0xef, 0xbf, 0xbd, 0x41, 0xef, 0xbf, 0xbd, 0x41, 0xc2, 0xa5],
  },
  {
    input: 'A\uDF06',
    read: 2,
    destinationLength: 4,
    written: [0x41, 0xef, 0xbf, 0xbd],
  },
  {
    input: '¥¥',
    read: 2,
    destinationLength: 4,
    written: [0xc2, 0xa5, 0xc2, 0xa5],
  },
].forEach((testData) => {
  [
    {
      bufferIncrease: 0,
      destinationOffset: 0,
      filler: 0,
    },
    {
      bufferIncrease: 10,
      destinationOffset: 4,
      filler: 0,
    },
    {
      bufferIncrease: 0,
      destinationOffset: 0,
      filler: 0x80,
    },
    {
      bufferIncrease: 10,
      destinationOffset: 4,
      filler: 0x80,
    },
    {
      bufferIncrease: 0,
      destinationOffset: 0,
      filler: 'random',
    },
    {
      bufferIncrease: 10,
      destinationOffset: 4,
      filler: 'random',
    },
  ].forEach((destinationData) => {
    ['ArrayBuffer', 'SharedArrayBuffer'].forEach((arrayBufferOrSharedArrayBuffer) => {
      test(() => {
        // Setup
        const bufferLength = testData.destinationLength + destinationData.bufferIncrease;
        const destinationOffset = destinationData.destinationOffset;
        const destinationLength = testData.destinationLength;
        const destinationFiller = destinationData.filler;
        const encoder = new TextEncoder();
        const buffer = createBuffer(arrayBufferOrSharedArrayBuffer, bufferLength);
        const view = new Uint8Array(buffer, destinationOffset, destinationLength);
        const fullView = new Uint8Array(buffer);
        const control = new Array(bufferLength);
        let byte = destinationFiller;
        for (let i = 0; i < bufferLength; i++) {
          if (destinationFiller === 'random') {
            byte = Math.floor(Math.random() * 256);
          }
          control[i] = byte;
          fullView[i] = byte;
        }

        // It's happening
        const result = encoder.encodeInto(testData.input, view);

        // Basics
        assert_equals(view.byteLength, destinationLength);
        assert_equals(view.length, destinationLength);

        // Remainder
        assert_equals(result.read, testData.read);
        assert_equals(result.written, testData.written.length);
        for (let i = 0; i < bufferLength; i++) {
          if (i < destinationOffset || i >= destinationOffset + testData.written.length) {
            assert_equals(fullView[i], control[i]);
          } else {
            assert_equals(fullView[i], testData.written[i - destinationOffset]);
          }
        }
      }, 'encodeInto() into ' + arrayBufferOrSharedArrayBuffer + ' with ' + testData.input + ' and destination length ' + testData.destinationLength + ', offset ' + destinationData.destinationOffset + ', filler ' + destinationData.filler);
    });
  });
});

[
  'DataView',
  'Int8Array',
  'Int16Array',
  'Int32Array',
  'Uint16Array',
  'Uint32Array',
  'Uint8ClampedArray',
  'BigInt64Array',
  'BigUint64Array',
  'Float32Array',
  'Float64Array',
].forEach((type) => {
  ['ArrayBuffer', 'SharedArrayBuffer'].forEach((arrayBufferOrSharedArrayBuffer) => {
    test(() => {
      const viewInstance = new self[type](createBuffer(arrayBufferOrSharedArrayBuffer, 0));
      assert_throws_js(TypeError, () => new TextEncoder().encodeInto('', viewInstance));
    }, 'Invalid encodeInto() destination: ' + type + ', backed by: ' + arrayBufferOrSharedArrayBuffer);
  });
});

['ArrayBuffer', 'SharedArrayBuffer'].forEach((arrayBufferOrSharedArrayBuffer) => {
  test(() => {
    assert_throws_js(TypeError, () =>
      new TextEncoder().encodeInto('', createBuffer(arrayBufferOrSharedArrayBuffer, 10)),
    );
  }, 'Invalid encodeInto() destination: ' + arrayBufferOrSharedArrayBuffer);
});

test(() => {
  const buffer = new ArrayBuffer(10),
    view = new Uint8Array(buffer);
  let { read, written } = new TextEncoder().encodeInto('', view);
  assert_equals(read, 0);
  assert_equals(written, 0);
  new MessageChannel().port1.postMessage(buffer, [buffer]);
  ({ read, written } = new TextEncoder().encodeInto('', view));
  assert_equals(read, 0);
  assert_equals(written, 0);
  ({ read, written } = new TextEncoder().encodeInto('test', view));
  assert_equals(read, 0);
  assert_equals(written, 0);
}, 'encodeInto() and a detached output buffer');

This then fails with a bunch of failed a assertions: Test failed with error Error: Assertion failed: 1 is not equal to 2. Commenting out the delete TextEncoder line then allows us to verify that the tests pass when executed with the native TextEncoder.

v8/js engine string cache use age

Hey Jack!

Thank you again for this amazing library!
I'n using parts of the code in https://github.com/Bnaya/objectbuffer

V8 and other js engines are trying to deduplicate strings references, and also hold strings as rope data structure.
In my use-case, i'm doing many decoding of the same string over and over again, and i'm not sure how to actual memory in the js engine side gonna end up.

I was wondering if you made any analysis regarding that topic and if you cloud share some findings

Thanks!
Bnaya

TextDecoder does not handle offset correctly

At https://github.com/anonyco/FastestSmallestTextEncoderDecoder/blob/master/individual/FastestTextDecoderPolyfill.src.js#L52 the input Uint8Array sometimes carry a non-zero byteOffset, which is needed to get the correct underlying ArrayBuffer.

> var aaa = new Uint8Array(new TextEncoder().encode("wrong hello").buffer, 6);
> new TextDecoder().decode(aaa);
"hello" // Chrome native
> new TextDecoderPolyfill().decode(aaa);
"wrong hello" // polyfill

A simple fix could be
var buffer = (inputArrayOrBuffer && inputArrayOrBuffer.buffer && inputArrayOrBuffer.buffer.slice(inputArrayOrBuffer.byteOffset, inputArrayOrBuffer.byteOffset + inputArrayOrBuffer.byteLength)) || inputArrayOrBuffer;
instead

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.