From 16c72e688f51208565e1bbe67345bf77de33d93f Mon Sep 17 00:00:00 2001 From: Shuhei Kagawa Date: Thu, 3 Jan 2019 06:28:40 +0100 Subject: [PATCH] Optimize percentile calculation (#67) --- lib/binary_heap.js | 7 ++----- stats/exponentially_decaying_sample.js | 10 ++-------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/binary_heap.js b/lib/binary_heap.js index 68ed13c..06a4285 100644 --- a/lib/binary_heap.js +++ b/lib/binary_heap.js @@ -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) { diff --git a/stats/exponentially_decaying_sample.js b/stats/exponentially_decaying_sample.js index 75931fd..9586840 100644 --- a/stats/exponentially_decaying_sample.js +++ b/stats/exponentially_decaying_sample.js @@ -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() {