Created
October 23, 2019 21:40
-
-
Save evanlimanto/07670a6eee03149fa149a1c004595a2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
})(); |
Author
evanlimanto
commented
Oct 23, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment