Skip to content

Instantly share code, notes, and snippets.

@mmorearty
Last active September 19, 2018 00:33
Show Gist options
  • Save mmorearty/c45d36626435b385549d72a9e236e338 to your computer and use it in GitHub Desktop.
Save mmorearty/c45d36626435b385549d72a9e236e338 to your computer and use it in GitHub Desktop.
const myset = new Set();
const myarray = [];
for (let i=0; i<1000 * 1000; i++) {
myset.add(i);
myarray.push(i);
}
function reportTime(label, callback) {
const start = process.hrtime();
callback();
const precision = 3;
const elapsed = process.hrtime(start)[1] / 1000000;
console.log(process.hrtime(start)[0] + " s, " + elapsed.toFixed(precision) + " ms - " + label);
}
reportTime("Set#has()", () => {
for (let i=0; i<1000 * 1000; i++) {
const r = Math.floor(Math.random() * 1000 * 1000);
myset.has(r);
}
});
reportTime("Array#includes()", () => {
for (let i=0; i<1000 * 1000; i++) {
const r = Math.floor(Math.random() * 1000 * 1000);
myarray.includes(r);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment