Skip to content

Instantly share code, notes, and snippets.

@x3ro
Created June 26, 2012 22:05
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 x3ro/2999594 to your computer and use it in GitHub Desktop.
Save x3ro/2999594 to your computer and use it in GitHub Desktop.
Assoc.js Benchmark
function createRandomWord(length) {
var consonants = 'bcdfghjklmnpqrstvwxyz',
vowels = 'aeiou',
rand = function(limit) {
return Math.floor(Math.random()*limit);
},
i, word='', length = parseInt(length,10),
consonants = consonants.split(''),
vowels = vowels.split('');
for (i=0;i<length/2;i++) {
var randConsonant = consonants[rand(consonants.length)],
randVowel = vowels[rand(vowels.length)];
word += (i===0) ? randConsonant.toUpperCase() : randConsonant;
word += i*2<length-1 ? randVowel : '';
}
return word;
}
var words = [];
for(var i=0; i<10000000; i++) {
words.push(createRandomWord(8))
}
console.log("Finished creating random words!")
// Uses requires benchmark.js v1.0.0-pre (benchmarkjs.com)
var Benchmark = require('benchmark'),
AssociativeArray = require('./../lib/assoc');
var suite = new Benchmark.Suite;
var arr = new AssociativeArray();
var wordcount = 0;
suite.add('#push performance', function() {
var word = words[wordcount];
wordcount++;
arr.push([[word, word]]);
})
suite.add('#assoc performance', function() {
var item = words[Math.floor(Math.random()*wordcount)];
arr.assoc(item);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log("Test created " + arr.length() + " elements");
})
.run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment