Skip to content

Instantly share code, notes, and snippets.

@heimskr
Last active February 8, 2019 11:48
Show Gist options
  • Save heimskr/39501fb8988179b612f248026051507a to your computer and use it in GitHub Desktop.
Save heimskr/39501fb8988179b612f248026051507a to your computer and use it in GitHub Desktop.
setperf
const int = 10000; // set to false to test with floats (disables inclusion tests)
const n = 10000;
const m = 100;
const rand = int? () => Math.floor(Math.random() * int) : Math.random;
const T = s => console.time(t = s);
const E = s => console.timeEnd(t);
let i, j, t;
if (int) {
console.log(`Random number generation: ints (0 ... ${int})`);
} else {
console.log(`Random number generation: floats`);
}
console.log("Iterations:", n);
console.log("Subiterations:", m);
console.log();
T("Generating arrays");
const arrs = [];
for (i = 0; i < n; i++) {
const arr = [];
for (j = 0; j < m; j++)
arr.push(rand());
arrs.push(arr);
}
E();
T("Generating sets");
const sets = [];
for (i = 0; i < n; i++) {
const set = new Set();
for (j = 0; j < m; j++)
set.add(rand());
sets.push(set);
}
E();
console.log();
T("Mapping arrays");
for (i = 0; i < n; i++) {
arrs[i] = arrs[i].map(x => x * 2);
}
E();
T("Mapping sets (Array.from)");
for (i = 0; i < n; i++) {
sets[i] = new Set(Array.from(sets[i]).map(x => x * 2));
}
E();
T("Mapping sets (spread operator)");
for (i = 0; i < n; i++) {
sets[i] = new Set([...sets[i]].map(x => x * 2));
}
E();
console.log();
T("Cloning arrays (slice)");
let arrs_ = [];
for (i = 0; i < n; i++) {
arrs_[i] = arrs[i].slice(0);
}
E();
T("Cloning arrays (spread operator)");
arrs_ = [];
for (i = 0; i < n; i++) {
arrs_[i] = [...arrs[i]];
}
E();
T("Cloning arrays (Array.from)");
arrs_ = [];
for (i = 0; i < n; i++) {
arrs_[i] = Array.from(arrs[i]);
}
E();
if (int) {
console.log();
T("Array inclusion");
for (i = 0; i < n; i++) {
const arr = arrs[i];
let c = 0;
for (j = 0; j < int; j++) {
if (arr.includes(j))
c++;
}
}
E();
T("Set inclusion");
for (i = 0; i < n; i++) {
const set = sets[i];
let c = 0;
for (j = 0; j < int; j++) {
if (set.has(j))
c++;
}
}
E();
}
console.log();
T("Emptying arrays (shift)");
for (i = 0; i < n; i++) {
const arr = arrs[i];
while (arr.length)
arr.shift();
}
E();
T("Emptying arrays (pop)");
for (i = 0; i < n; i++) {
const arr = arrs_[i];
while (arr.length)
arr.pop();
}
E();
T("Emptying sets");
for (i = 0; i < n; i++) {
const set = sets[i];
const values = set.values();
let v;
do {
set.delete((v = set.values().next()).value);
} while (!v.done);
}
E();
@heimskr
Copy link
Author

heimskr commented Feb 8, 2019

My results (nodejs v10.6.0, macOS 10.14):

Random number generation: ints (0 ... 10000)
Iterations: 10000
Subiterations: 100

Generating arrays: 70.847ms
Generating sets: 123.601ms

Mapping arrays: 28.065ms
Mapping sets (Array.from): 262.420ms
Mapping sets (spread operator): 107.516ms

Cloning arrays (slice): 7.448ms
Cloning arrays (spread operator): 28.053ms
Cloning arrays (Array.from): 189.455ms

Array inclusion: 13918.812ms
Set inclusion: 2213.377ms

Emptying arrays (shift): 125.746ms
Emptying arrays (pop): 12.218ms
Emptying sets: 130.925ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment