Skip to content

Commit

Permalink
Optimize percentile calculation (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhei authored and tolbertam committed Jan 3, 2019
1 parent c0f9e33 commit 16c72e6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
7 changes: 2 additions & 5 deletions lib/binary_heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ var BinaryHeap = module.exports = function BinaryHeap(scoreFunction){

BinaryHeap.prototype = {

clone: function() {
var heap = new BinaryHeap(this.scoreFunction);
// A little hacky, but effective.
heap.content = JSON.parse(JSON.stringify(this.content));
return heap;
getValues: function() {
return this.content;
},

push: function(element) {
Expand Down
10 changes: 2 additions & 8 deletions stats/exponentially_decaying_sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,9 @@ var ExponentiallyDecayingSample = module.exports = function ExponentiallyDecayin

ExponentiallyDecayingSample.prototype = new Sample();

// This is a relatively expensive operation
// The result is not sorted in a meaningful order.
ExponentiallyDecayingSample.prototype.getValues = function() {
var values = []
, elt
, heap = this.values.clone();
while(elt = heap.pop()) {
values.push(elt.val);
}
return values;
return this.values.getValues().map(function(v) { return v.val; });
}

ExponentiallyDecayingSample.prototype.size = function() {
Expand Down

0 comments on commit 16c72e6

Please sign in to comment.