Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize percentile calculation #67

Merged
merged 3 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that is odd to me is why it does:

heap.content = JSON.parse(JSON.stringify(this.content));

This looks really inefficient, instead this should work better:

heap.content = this.content.slice(0);

I updated percentiles.js benchmark to try this out, changing the cycle method to:

  .on("cycle", function(event) {
        console.log(String(event.target));
        BinaryHeap.prototype.clone = function() {
          var heap = new BinaryHeap(this.scoreFunction);
          heap.content = this.content.slice(0);
          return heap;
        };
      })

Results:

original x 755 ops/sec ±2.86% (89 runs sampled)
updated x 3,365 ops/sec ±2.01% (93 runs sampled)

The performance is not as good as the non-copy approach, but it's definitely better than before. If we go the route of adding the snapshot parameter, we can retain a clone implementation and change it to use slice instead of using the JSON trick.

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; });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'oh, now that I think of it, this very line here of using map and returning the underlying value works fine! We still essentially get a snapshot, so ignore my previous comments as this is sufficient. Sorry for any confusion!

}

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