Skip to content

Instantly share code, notes, and snippets.

@evanlimanto
Created October 23, 2019 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanlimanto/07670a6eee03149fa149a1c004595a2c to your computer and use it in GitHub Desktop.
Save evanlimanto/07670a6eee03149fa149a1c004595a2c to your computer and use it in GitHub Desktop.
import * as b64 from 'b64';
import * as bfj from 'bfj';
import * as P from 'bluebird';
import * as bench from 'fastbench';
import * as fs from 'fs';
import * as jsonstream from 'JSONStream';
import * as zlib from 'zlib';
import {
streamToString,
} from '../framework/utils/streams';
const plars: Array<string> = [];
for (let i = 0; i < 10; i++) {
if (i === 8)
continue;
const name = (21524251059 + i).toString();
const contents =
fs.readFileSync(`/Users/elimanto/Downloads/${name}.json`).toString();
plars.push(JSON.parse(contents.trim()));
}
export const bfjGenerate = async (
plar: any,
): Promise<string> => {
return await streamToString(
bfj.streamify(plar)
.pipe(zlib.createDeflate())
.pipe(new b64.Encoder()),
);
};
export const jsonStreamGenerate = async (
plar: any,
): Promise<string> => {
const ss = jsonstream.stringify(false);
const piper =
ss.pipe(zlib.createDeflate()).pipe(new b64.Encoder());
ss.write(plar as any);
ss.end();
return streamToString(piper);
};
(async () => {
const run = bench([
function benchBFJ(cb: any) {
P.map(plars, bfjGenerate).then(cb);
},
function benchJSONStream(cb: any) {
P.map(plars, jsonStreamGenerate).then(cb);
},
], 100);
run(run);
})();
@evanlimanto
Copy link
Author

benchBFJ*100: 67652.616ms
benchJSONStream*100: 14094.825ms
benchBFJ*100: 65582.191ms
benchJSONStream*100: 12166.841ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment