Last active
February 8, 2019 11:48
-
-
Save heimskr/39501fb8988179b612f248026051507a to your computer and use it in GitHub Desktop.
setperf
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
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My results (nodejs v10.6.0, macOS 10.14):