Skip to content

Instantly share code, notes, and snippets.

@zbicin
Last active October 6, 2020 17:50
Show Gist options
  • Save zbicin/13675069abab716498cd53ee63543035 to your computer and use it in GitHub Desktop.
Save zbicin/13675069abab716498cd53ee63543035 to your computer and use it in GitHub Desktop.
Shapes and IC test

Results

Each test was performed 5 times and an average time was calculated.

Optimized Size Average time [ms] Heap size [MB]
10e2 0.06158013343811035 3.1507919311523436
10e4 2.3224000930786133 10.012129211425782
10e6 246.5065408229828 487.14283142089846
✔️ 10e2 0.05734000205993652 2.86851806640625
✔️ 10e4 0.9794797897338867 3.3013275146484373
✔️ 10e6 33.00432024002075 78.09739990234375

Average time

Heap size

const { performance } = require("perf_hooks");
let factory = process.argv.includes("--optimized")
? buildStandarizedObject
: buildRandomlyShapedObject;
let size = 10e4;
const sizeOptionPrefix = "--size=";
if (process.argv.find((part) => part.startsWith(sizeOptionPrefix))) {
size = +process.argv
.find((part) => part.startsWith(sizeOptionPrefix))
.substr(sizeOptionPrefix.length);
}
function getX(obj) {
return obj.x;
}
function buildRandomlyShapedObject(x) {
const obj = {};
obj[reallyRandom()] = reallyRandom();
obj.x = x;
obj[reallyRandom()] = reallyRandom();
return obj;
}
function buildStandarizedObject(x) {
return { foo: reallyRandom(), x };
}
let lastRandom = 0;
function reallyRandom() {
let random = lastRandom;
do {
random = Math.random();
} while (random === lastRandom);
lastRandom = random;
return random;
}
console.log(`Building ${size} elements using ${factory.name} function`);
const objs = new Array(size);
for (let i = 0; i < size; i++) {
objs[i] = factory(i);
}
const start = performance.now();
objs.forEach((obj) => getX(obj));
const end = performance.now();
console.log(end - start);
console.log(`Heap used: ${process.memoryUsage().heapUsed / 1024 / 1024} MB`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment