Skip to content

Instantly share code, notes, and snippets.

@youurayy
Created February 22, 2012 19:02
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 youurayy/1886673 to your computer and use it in GitHub Desktop.
Save youurayy/1886673 to your computer and use it in GitHub Desktop.
v8/node collections benchmark (Array, Object, ES6 Set)
var assert = require('assert');
var len = 100000;
// create test clients, just empty objects
var sampleClients = [];
for(i = 0; i < len; i++)
sampleClients.push({ id: i });
// create a randomized list for tests
var randomizedClients = sampleClients.slice();
for(i = 0; i < len; i++) {
var j = Math.floor(Math.random() * len);
var k = Math.floor(Math.random() * len);
var s = randomizedClients[j];
randomizedClients[j] = randomizedClients[k];
randomizedClients[k] = s;
}
function array_list() {
var ws = { _clients: [] };
// add the clients
for(i = 0; i < len; i++)
ws._clients.push(sampleClients[i]);
// remove clients one by one
for(i = 0; i < len; i++) {
var client = randomizedClients[i];
var idx = ws._clients.indexOf(client);
if(idx != -1)
ws._clients.splice(idx, 1);
}
assert(ws._clients.length === 0);
ws = null;
}
function object_map() {
var ws = { _clients: new Object(null) };
// add the clients
for(i = 0; i < len; i++) {
var client = sampleClients[i];
ws._clients[client.id] = client;
}
// remove clients one by one
for(i = 0; i < len; i++) {
var client = randomizedClients[i];
delete ws._clients[client.id];
}
function is_empty(obj) { for(var i in obj) return false; return true; }
assert(is_empty(ws._clients));
ws = null;
}
//--harmony_collections
function es6_set() {
// Harmony Set Access
var ws = { _clients: new Set() };
// add the clients
for(i = 0; i < len; i++) {
ws._clients.add(sampleClients[i]);
}
// remove clients one by one
for(i = 0; i < len; i++) {
var client = randomizedClients[i];
ws._clients.delete(client.id);
}
assert(Object.keys(ws._clients).length === 0);
ws = null;
}
function test(func) {
var s = Date.now();
func();
var e = Date.now();
var dur = e - s;
var msop = dur / (len * 2); // millis per operation (we've got create & delete)
console.log(func.name + ': ' + dur + ' ms (' + msop + ' ms/op)');
if(typeof(gc) !== 'undefined') // --expose-gc
gc();
}
var delay = 3000;
function testall(cnt) {
if(cnt == 0)
return;
test(array_list);
setTimeout(function() {
test(object_map);
if(typeof(Set) !== 'undefined') {
setTimeout(function() {
test(es6_set);
console.log('---------------------');
testall(--cnt);
}, delay);
}
else {
console.log('---------------------');
testall(--cnt);
}
}, delay);
}
console.log('---------------------');
testall(3);
@youurayy
Copy link
Author

node v0.6.10, 10k objects:

---------------------
array_list: 101 ms (0.00505 ms/op)
object_map: 5 ms (0.00025 ms/op)
---------------------
array_list: 81 ms (0.00405 ms/op)
object_map: 2 ms (0.0001 ms/op)
---------------------
array_list: 83 ms (0.00415 ms/op)
object_map: 2 ms (0.0001 ms/op)
---------------------

node v0.6.10, 100k objects:

---------------------
array_list: 7990 ms (0.03995 ms/op)
object_map: 41 ms (0.000205 ms/op)
---------------------
array_list: 12009 ms (0.060045 ms/op)
object_map: 4284 ms (0.02142 ms/op)
---------------------
array_list: 10380 ms (0.0519 ms/op)
object_map: 4280 ms (0.0214 ms/op)
---------------------

node v0.7.4, 10k objects:

---------------------
array_list: 86 ms (0.0043 ms/op)
object_map: 5 ms (0.00025 ms/op)
es6_set: 21 ms (0.00105 ms/op)
---------------------
array_list: 150 ms (0.0075 ms/op)
object_map: 5 ms (0.00025 ms/op)
es6_set: 7 ms (0.00035 ms/op)
---------------------
array_list: 157 ms (0.00785 ms/op)
object_map: 3 ms (0.00015 ms/op)
es6_set: 5 ms (0.00025 ms/op)
---------------------

node v0.7.4, 100k objects:

---------------------
array_list: 9574 ms (0.04787 ms/op)
object_map: 34 ms (0.00017 ms/op)
es6_set: 377 ms (0.001885 ms/op)
---------------------
array_list: 23836 ms (0.11918 ms/op)
object_map: 3151 ms (0.015755 ms/op)
es6_set: 87 ms (0.000435 ms/op)
---------------------
array_list: 23819 ms (0.119095 ms/op)
object_map: 3148 ms (0.01574 ms/op)
es6_set: 87 ms (0.000435 ms/op)
---------------------

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