Skip to content

Instantly share code, notes, and snippets.

@joncloud
Last active December 6, 2016 15:01
Show Gist options
  • Select an option

  • Save joncloud/4b8e2defb811a1042373a60df5bd0a90 to your computer and use it in GitHub Desktop.

Select an option

Save joncloud/4b8e2defb811a1042373a60df5bd0a90 to your computer and use it in GitHub Desktop.
(function (total) {
times = []
function time(name, fn) {
var t0 = performance.now();
fn();
var t1 = performance.now();
times.push(name + " took " + (t1 - t0));
}
var array = [];
var set = new Set();
var hash = {};
time("array.push", function() {
i = total;
while(--i) {
array.push(i);
}
});
time("set.add", function() {
//i = total;
for (var i = 1; i < total; i++) {
set.add(i);
}
});
time("hash[i]", function() {
i = total;
while(--i) {
hash[i] = 1;
}
});
time("array.findIndex", function() {
console.log(array.findIndex(id => id == 5000) >= 0);
});
time("set.has", function() {
console.log(set.has(5000));
});
time("hash[i]", function() {
console.log(!!hash[5000]);
});
console.log(times);
})(100000);
"array.push took 1.9200000000000728"
"set.add took 56.220000000001164"
"hash[i] took 5.149999999999636"
"array.findIndex took 1.9350000000013097"
"set.has took 0.09999999999854481"
"hash[i] took 0.09499999999934516"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment