Skip to content

Commit

Permalink
rewrite to polyfill of TextEncoder/TextDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
samthor committed Sep 1, 2017
1 parent 093aae9 commit 1ea7660
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 159 deletions.
101 changes: 0 additions & 101 deletions js-utf.js

This file was deleted.

84 changes: 26 additions & 58 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,35 @@
import 'https://cdn.rawgit.com/inexorabletash/text-encoding/b98ab30b/lib/encoding.js';
const polyfill = module.exports;

import * as utf from './js-utf.js';
import './text.js';

const runs = 100;
const runs = 1;
const dataUrl = './utf8_sequence_0-0xffff_assigned_printable.txt';

(async function() {
let text = await window.fetch(dataUrl).then((data) => data.text());
text = text.substr(0, 10000);

function testEncodeDecode(name, tenc, tdec) {
console.time(name + '.TextEncoder');
let saved;
const encoder = new tenc();
for (let i = 0; i < runs; ++i) {
const out = encoder.encode(text);
saved = out;
}
console.info('got output', saved);
console.timeEnd(name + '.TextEncoder');

console.time(name + '.TextDecoder');
const decoder = new tdec();
for (let i = 0; i < runs; ++i) {
const out = decoder.decode(saved);
output = out;
}
console.timeEnd(name + '.TextDecoder');
}

let saved, output;

console.time('utf8.encode');
Expand All @@ -36,63 +57,10 @@
throw new Error('utf8 got wrong answer');
}

console.time('js-utf.encode');
const out = new Uint8Array(1024 * 1024);
for (let i = 0; i < runs; ++i) {
const len = utf.encode(text, out);
saved = out.slice(0, len);
}
console.timeEnd('js-utf.encode');
console.info(saved);
const polyfillSaved = saved;

console.time('js-utf.decode');
for (let i = 0; i < runs; ++i) {
const out = utf.decode(saved);
output = out;
}
console.timeEnd('js-utf.decode');
if (output !== text) {
console.info('expected output length', text.length, 'was', output.length);
throw new Error('js-utf got wrong answer');
}


console.time('TextEncoder');
const encoder = new TextEncoder();
for (let i = 0; i < runs; ++i) {
const out = encoder.encode(text);
saved = out;
}
console.timeEnd('TextEncoder');
console.info(saved);

console.time('TextDecoder');
const decoder = new TextDecoder();
for (let i = 0; i < runs; ++i) {
const out = decoder.decode(polyfillSaved);
output = out;
}
console.timeEnd('TextDecoder');


console.time('polyfill.TextEncoder');
const encoderP = new polyfill.TextEncoder();
for (let i = 0; i < runs; ++i) {
const out = encoderP.encode(text);
saved = out;
}
console.timeEnd('polyfill.TextEncoder');
console.info(saved);

console.time('polyfill.TextDecoder');
const decoderP = new polyfill.TextDecoder();
for (let i = 0; i < runs; ++i) {
const out = decoderP.decode(polyfillSaved);
output = out;
}
console.timeEnd('polyfill.TextDecoder');

testEncodeDecode('native', TextEncoder, TextDecoder);
testEncodeDecode('fast', TextEncoderPolyfill, TextDecoderPolyfill);
testEncodeDecode('polyfill', polyfill.TextEncoder, polyfill.TextDecoder);

}());

Expand Down
Loading

0 comments on commit 1ea7660

Please sign in to comment.