From db51cdf8adc0358aeecfca1f3a5304580305a051 Mon Sep 17 00:00:00 2001 From: Robert Konigsberg Date: Sat, 16 Jun 2012 09:09:53 -0400 Subject: [PATCH] Decent optimziation by precomputing hasNext, saves about 3% --- dygraph-utils.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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_(); -- 2.7.4