function createDatabase(isIndexed) {
  var idx, a, b;
  
  db = new loki('sorting-bench.db');
   
  // we will always apply index to our filter column.
  // our variation will be whether the sort column is also indexed.
  if (isIndexed) {   
    coll = db.addCollection('profile', { indices: ['a', 'b'] });
  }
  else {
    coll = db.addCollection('profile', { indices: ['a'] });
  }
  
  for(idx=0;idx<DOCUMENT_COUNT;idx++) {
    a = Math.floor(Math.random() * QUERY_INDEX_RANGE);
    b = Math.floor(Math.random() * SORTING_INDEX_RANGE);
    coll.insert({ "a": a, "b": b });
  }
}
Sorting benchmark for comparing performance of sorting in various conditions.

This benchmark was added to benchmark benefits of leveraging binary indices
to sort results -after- filtering had occurred.

Typically, only the first indexed operation is able to use indices.

This benchmark will assume we are filtering at least 1 document and attempting
to perform a simplesort.

Since we are working with a filtered resultset, in order to leverage the 
sorting index we have applied on a separate property we will employ 
a devised set intersection algorithm to reduce the (full) sort property binary index
by the actual documents in our resultset.  

The mechanism we are using to optimally intersect arrays involves creating an 
'inclusion' (hash) object for document indices in our resultset and then 
filtering (array scan) the sort property binary index, testing for each element's
inclusion in our inclusion object.

Initial testing seems to indicate that our new algorithm is typically more efficient
than our previous simplesort() as long you were not able to filter more than 10% of 
total collection documents.  It's performance seems be directly correlated with 
your filtering ineffectiveness.  That means that the performance of the new simplesort 
over the old simplesort algorithm is greater when you were only able to filter 20% 
of total documents than if you were able to filter 80%).

Since the roughly "10% or more" filtering rule seems to apply no matter how 
large dataset is, we have created another simplesort implementation which 
calculates ratio of current resultset documents to total documents and 
invokes either the old 'sort' codepath or the new index intersect algorithm.