From: Robert Konigsberg Date: Sat, 16 Jun 2012 13:09:53 +0000 (-0400) Subject: Decent optimziation by precomputing hasNext, saves about 3% X-Git-Tag: v1.0.0~238^2^2~8 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=db51cdf8adc0358aeecfca1f3a5304580305a051;p=dygraphs.git Decent optimziation by precomputing hasNext, saves about 3% --- diff --git a/dygraph-utils.js b/dygraph-utils.js index c7b52d2..f65a7a6 100644 --- a/dygraph-utils.js +++ b/dygraph-utils.js @@ -705,13 +705,15 @@ Dygraph.createIterator = function(array, start, length, predicate) { this.idx_ = start - 1; // use -1 so initial call to advance works. this.end_ = Math.min(array.length, start + length); this.nextIdx_ = this.idx_; + this.hasNext_ = true; var self = this; this.hasNext = function() { - return self.nextIdx_ < self.end_; + return self.hasNext_; } + this.next = function() { - if (self.hasNext()) { + if (self.hasNext_) { self.idx_ = self.nextIdx_; self.advance_(); return array[self.idx_]; @@ -719,19 +721,20 @@ Dygraph.createIterator = function(array, start, length, predicate) { return null; } this.peek = function() { - if (self.hasNext()) { + if (self.hasNext_) { return array[self.nextIdx_]; } return null; } this.advance_ = function() { self.nextIdx_++; - while(self.hasNext()) { + while(self.nextIdx_ < self.end_) { if (predicate(array, self.nextIdx_)) { return; } self.nextIdx_++; } + self.hasNext_ = false; } }; iter.advance_();