Skip to content

Instantly share code, notes, and snippets.

@Mumakil
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mumakil/b0194011429c037e1833 to your computer and use it in GitHub Desktop.
Save Mumakil/b0194011429c037e1833 to your computer and use it in GitHub Desktop.
Javascript set benchmarks
var collectionCount = process.env['COLLECTIONS'] || 1000;
var perCollection = process.env['PER_COLLECTION'] || 1000;
var iterationCount = process.env['ITERATION_COUNT'] || 1000;
var collections = [];
var current, i, j, k, l, r;
for (i = 0; i < collectionCount; i += 1) {
current = [];
for (j = 0; j < perCollection; j += 1) {
current.push(String(j));
}
collections.push(current);
}
console.time(iterationCount + ' iterations with Array');
for (i = 0; i < iterationCount; i += 1) {
r = "" + Math.floor(Math.random() * perCollection);
for (j = 0; j < collections.length; j += 1) {
l = collections[j].length;
for (k = 0; k < l; k += 1) {
if (collections[j][k] == r) {
break;
}
}
}
}
console.timeEnd(iterationCount + ' iterations with Array');
var collectionCount = process.env['COLLECTIONS'] || 1000;
var perCollection = process.env['PER_COLLECTION'] || 1000;
var iterationCount = process.env['ITERATION_COUNT'] || 1000;
var collections = [];
var current, i, j, r;
for (i = 0; i < collectionCount; i += 1) {
current = {};
for (j = 0; j < perCollection; j += 1) {
current[j] = true;
}
collections.push(current);
}
console.time(iterationCount + ' iterations with Hash');
for (i = 0; i < iterationCount; i += 1) {
r = "" + Math.floor(Math.random() * perCollection);
for (j = 0; j < collections.length; j += 1) {
!!collections[j]["" + r];
}
}
console.timeEnd(iterationCount + ' iterations with Hash');
[vehvis:~/Code/node-benchmark][ruby-2.2.1] [node iojs-v1.6.2] $
↳ PER_COLLECTION=5000 node array_lookup.js && PER_COLLECTION=5000 node hash_lookup.js && PER_COLLECTION=5000 node set_lookup.js
1000 iterations with Array: 18396ms
1000 iterations with Hash: 37ms
1000 iterations with Set: 129ms
[vehvis:~/Code/node-benchmark][ruby-2.2.1] [node v0.12.1] $
↳ PER_COLLECTION=5000 node array_lookup.js && PER_COLLECTION=5000 node hash_lookup.js && PER_COLLECTION=5000 node set_lookup.js
1000 iterations with Array: 17949ms
1000 iterations with Hash: 38ms
1000 iterations with Set: 254ms
var collectionCount = process.env['COLLECTIONS'] || 1000;
var perCollection = process.env['PER_COLLECTION'] || 1000;
var iterationCount = process.env['ITERATION_COUNT'] || 1000;
var collections = [];
var current, i, j, r;
for (i = 0; i < collectionCount; i += 1) {
current = new Set();
for (j = 0; j < perCollection; j += 1) {
current.add(String(j));
}
collections.push(current);
}
console.time(iterationCount + ' iterations with Set');
for (i = 0; i < iterationCount; i += 1) {
r = "" + Math.floor(Math.random() * perCollection);
for (j = 0; j < collections.length; j += 1) {
collections[j].has(r);
}
}
console.timeEnd(iterationCount + ' iterations with Set');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment