Skip to content

Instantly share code, notes, and snippets.

@metric-space
Created November 7, 2015 18:07
Show Gist options
  • Save metric-space/7d860e94f6889feec4e6 to your computer and use it in GitHub Desktop.
Save metric-space/7d860e94f6889feec4e6 to your computer and use it in GitHub Desktop.
filling and searching objects, arrays and red-black trees with date strings
var _ = require('lodash');
var moment = require('moment');
var RBTree = require('bintrees').RBTree;
var time = require('performance-now');
var TEST_NUMBER = 100000
function randomRangeGenerator(min,max){
var a = Math.floor(min + Math.random()*(max-min));
if (String(a).length == 1 ){
a = '0'+a
}
return a;
}
function dateStringGenerator(){
var r = randomRangeGenerator;
return r(2010,2015)+'-'+r(1,12)+'-'+r(1,28);
}
function timeStringGenerator(){
var r = randomRangeGenerator;
return 'T'+r(0,24)+':'+r(0,60)+':'+r(0,60);
}
function generateRandomDate(){
return dateStringGenerator()+timeStringGenerator();
}
//-----------------------------------------------------------
var arrayForTest = []
var objectForTest = {}
var tree = new RBTree(function(a,b){
var a = moment(a);
return a.isSame(b)?0:a.isBefore(b)?-1:1;
})
//============================================================
//
// FILLING STUFF UP
//
//============================================================
function fillEmUp(dataTypeString,pushFunction){
console.log("filling up "+dataTypeString+" with "+ TEST_NUMBER+"values")
var t0 = time();
_(TEST_NUMBER)
.range()
.map(function(a){
var randomDateString = generateRandomDate()
pushFunction(randomDateString);
})
.value()
var t1 = time();
console.log("filling up the "+dataTypeString+" took "+(t1-t0))
}
fillEmUp('array',function(rand){ arrayForTest.push(rand); });
console.log(arrayForTest);
fillEmUp('object',function(rand) { objectForTest[rand] = 1; });
fillEmUp('btree',function(rand){ tree.insert(rand); })
// ===================================================================
// Searching stuff up
//=====================================================================
//
var testDateString = arrayForTest[789];
var t0 = time();
arrayForTest.indexOf(testDateString)
var t1 = time()
console.log("array search took "+(t1-t0))
var t2 = time();
objectForTest[testDateString]
var t3 = time()
console.log("object search took "+(t3-t2))
var t4 = time();
tree.find(testDateString)
var t5 = time()
console.log("rbtree search took "+(t5-t4))
//=====================================================================
// Bounded Search
//===================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment