Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@evanlimanto evanlimanto commented Oct 23, 2019

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
You can’t perform that action at this time.