Skip to content

Instantly share code, notes, and snippets.

@justinvdm
Last active May 21, 2020 13:31
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 justinvdm/eaae3a59c1a1790704db9674e1785afa to your computer and use it in GitHub Desktop.
Save justinvdm/eaae3a59c1a1790704db9674e1785afa to your computer and use it in GitHub Desktop.
// !!! NOTE !!!
// This is *not* meant to be a comparison to show which library is
// universally faster. In fact, it is quite likely that faker and
// chance have functions that perform better than the equivalents in
// fictional. The 'seed each' benchmarks below are probably also
// misuse of faker and chance. The intention of these benchmarks was
// to investigate and show why seeding as a way of mapping
// identifiers to generated values isn't viable, and why hashing is a
// promising solution
const Benchmark = require("benchmark");
const faker = require("faker");
const Chance = require("chance");
const f = require("fictional");
displayIntro();
const N = 1000;
const chance = new Chance(23);
faker.seed(23);
new Benchmark.Suite("fictional-faker-chance-seeding", {
onComplete(e) {
this.map((bench) =>
console.log(
JSON.stringify({
name: bench.name,
hz: bench.hz.toFixed(2),
relativeMarginOfError: `${bench.stats.rme.toFixed(2)}%`,
runs: bench.stats.sample.length,
})
)
);
},
onError(e) {
console.error(e.target.error);
},
})
.add("chance: seeded", () => {
let i = -1;
while (++i < N) chance.integer();
})
.add("chance: seed each", () => {
let i = -1;
while (++i < N) new Chance(23).integer();
})
.add("faker: seeded", () => {
let i = -1;
while (++i < N) faker.random.number();
})
.add("faker: seed each", () => {
let i = -1;
while (++i < N) {
faker.seed(23);
faker.random.number();
}
})
.add("fictional", () => {
let i = -1;
while (++i < N) f.int(23);
})
.run();
function displayIntro() {
console.log(JSON.stringify({ nodeVersion: process.version }));
}
{"nodeVersion":"v12.16.2"}
{"name":"chance: seeded","hz":"413.03","relativeMarginOfError":"100.44%","runs":51}
{"name":"chance: seed each","hz":"13.99","relativeMarginOfError":"4.60%","runs":26}
{"name":"faker: seeded","hz":"1936.07","relativeMarginOfError":"13.73%","runs":35}
{"name":"faker: seed each","hz":"1.17","relativeMarginOfError":"2.07%","runs":7}
{"name":"fictional","hz":"4099.55","relativeMarginOfError":"4.85%","runs":56}
{
"name": "bench-fictional-faker-chance-seeding",
"version": "1.0.0",
"description": "",
"main": "_bench-fictional-faker-chance-seeding.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"bench": "node _bench-fictional-faker-chance-seeding.js | tee _results.ldjson"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"benchmark": "^2.1.4",
"chance": "^1.1.4",
"faker": "^4.1.0",
"fictional": "^0.4.0"
}
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
benchmark@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/benchmark/-/benchmark-2.1.4.tgz#09f3de31c916425d498cc2ee565a0ebf3c2a5629"
integrity sha1-CfPeMckWQl1JjMLuVloOvzwqVik=
dependencies:
lodash "^4.17.4"
platform "^1.3.3"
chance@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/chance/-/chance-1.1.4.tgz#d8743bf8e40bb05e024c305ca1ff441195eb23db"
integrity sha512-pXPDSu3knKlb6H7ahQfpq//J9mSOxYK8SMtp8MV/nRJh8aLRDIl0ipLH8At8+nVogVwtvPZzyIzY/EbcY/cLuQ==
faker@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/faker/-/faker-4.1.0.tgz#1e45bbbecc6774b3c195fad2835109c6d748cc3f"
integrity sha1-HkW7vsxndLPBlfrSg1EJxtdIzD8=
fast-json-stable-stringify@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
fictional@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/fictional/-/fictional-0.4.0.tgz#ff00dd65852d810c68477a8034654fb97f730d1c"
integrity sha512-d8+QeOhLdj+lE17Lx7dAv6/LKM4ZgoAEw7byhWEzStOfY21eFC3Rx6mKayLl3wHWnMgxykB+/NYB8WxsglPR3Q==
dependencies:
fast-json-stable-stringify "^2.1.0"
string-hash "^1.1.3"
lodash@^4.17.4:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
platform@^1.3.3:
version "1.3.5"
resolved "https://registry.yarnpkg.com/platform/-/platform-1.3.5.tgz#fb6958c696e07e2918d2eeda0f0bc9448d733444"
integrity sha512-TuvHS8AOIZNAlE77WUDiR4rySV/VMptyMfcfeoMgs4P8apaZM3JrnbzBiixKUv+XR6i+BXrQh8WAnjaSPFO65Q==
string-hash@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/string-hash/-/string-hash-1.1.3.tgz#e8aafc0ac1855b4666929ed7dd1275df5d6c811b"
integrity sha1-6Kr8CsGFW0Zmkp7X3RJ1311sgRs=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment