From 5442f87cb2cff7eaf1cebc52370c39ee59eb171b Mon Sep 17 00:00:00 2001 From: Robert Konigsberg Date: Sat, 16 Jun 2012 09:26:17 -0400 Subject: [PATCH] Iterator optimization - peek-ahead is optimized, and, remove idx_, which is not required. --- dygraph-utils.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/dygraph-utils.js b/dygraph-utils.js index f65a7a6..0b9d2e5 100644 --- a/dygraph-utils.js +++ b/dygraph-utils.js @@ -702,10 +702,10 @@ Dygraph.createIterator = function(array, start, length, predicate) { predicate = predicate || function() { return true; }; var iter = new function() { - 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.nextIdx_ = start - 1; // use -1 so initial call to advance works. this.hasNext_ = true; + this.peek_ = null; var self = this; this.hasNext = function() { @@ -714,27 +714,26 @@ Dygraph.createIterator = function(array, start, length, predicate) { this.next = function() { if (self.hasNext_) { - self.idx_ = self.nextIdx_; + var obj = self.peek_; self.advance_(); - return array[self.idx_]; + return obj; } return null; } this.peek = function() { - if (self.hasNext_) { - return array[self.nextIdx_]; - } - return null; + return self.peek_; } this.advance_ = function() { self.nextIdx_++; while(self.nextIdx_ < self.end_) { if (predicate(array, self.nextIdx_)) { + self.peek_ = array[self.nextIdx_]; return; } self.nextIdx_++; } self.hasNext_ = false; + self.peek_ = null; } }; iter.advance_(); -- 2.7.4