Created
February 22, 2012 19:02
-
-
Save youurayy/1886673 to your computer and use it in GitHub Desktop.
v8/node collections benchmark (Array, Object, ES6 Set)
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
node v0.6.10, 10k objects:
node v0.6.10, 100k objects:
node v0.7.4, 10k objects:
node v0.7.4, 100k objects: