From f27f35268a29e9646f9ecfae9d80347c18370edc Mon Sep 17 00:00:00 2001 From: George Lund Date: Fri, 3 Feb 2012 17:45:30 +0000 Subject: [PATCH] Performance fixes: create class methods using Class.prototype, rather than in constructor. --- static/scripts/jquery.openheatmap.js | 536 +++++++++++++-------------- 1 file changed, 263 insertions(+), 273 deletions(-) diff --git a/static/scripts/jquery.openheatmap.js b/static/scripts/jquery.openheatmap.js index f8b9ba3..d97b8a5 100644 --- a/static/scripts/jquery.openheatmap.js +++ b/static/scripts/jquery.openheatmap.js @@ -4479,333 +4479,323 @@ function Rectangle(x, y, width, height) this.y = y; this.width = width; this.height = height; - - this.bottom = function(newY) { - if (typeof newY !== 'undefined') - this.height = (newY-this.y); - return (this.y+this.height); - }; - this.bottomRight = function() { - return new Point(this.right(), this.bottom()); - }; + return this; +} +Rectangle.prototype.bottom = function(newY) { + if (typeof newY !== 'undefined') + this.height = (newY-this.y); + return (this.y+this.height); +}; - this.left = function(newX) { - if (typeof newX !== 'undefined') - { - this.width += (this.x-newX); - this.x = newX; - } - return this.x; - }; - - this.right = function(newX) { - if (typeof newX !== 'undefined') - this.width = (newX-this.x); - return (this.x+this.width); - }; +Rectangle.prototype.bottomRight = function() { + return new Point(this.right(), this.bottom()); +}; + +Rectangle.prototype.left = function(newX) { + if (typeof newX !== 'undefined') + { + this.width += (this.x-newX); + this.x = newX; + } + return this.x; +}; + +Rectangle.prototype.right = function(newX) { + if (typeof newX !== 'undefined') + this.width = (newX-this.x); + return (this.x+this.width); +}; - this.size = function() { - return new Point(this.width, this.height); - }; +Rectangle.prototype.size = function() { + return new Point(this.width, this.height); +}; +Rectangle.prototype.top = function(newY) { + if (typeof newY !== 'undefined') + { + this.height += (this.y-newY); + this.y = newY; + } + return this.y; +}; + +Rectangle.prototype.topLeft = function() { + return new Point(this.x, this.y); +}; + +Rectangle.prototype.clone = function() { + return new Rectangle(this.x, this.y, this.width, this.height); +}; + +Rectangle.prototype.contains = function(x, y) { + var isInside = + (x>=this.x)&& + (y>=this.y)&& + (x=this.x)&& + (rect.y>=this.y)&& + (rect.right()<=this.right())&& + (rect.bottom()<=this.bottom()); + return isInside; +}; - this.top = function(newY) { - if (typeof newY !== 'undefined') - { - this.height += (this.y-newY); - this.y = newY; - } - return this.y; - }; +Rectangle.prototype.equals = function(toCompare) { + var isIdentical = + (toCompare.x===this.x)&& + (toCompare.y===this.y)&& + (toCompare.width===this.width)&& + (toCompare.height===this.height); + return isIdentical; +}; - this.topLeft = function() { - return new Point(this.x, this.y); - }; +Rectangle.prototype.inflate = function(dx, dy) { + this.x -= dx; + this.y -= dy; + this.width += (2*dx); + this.height += (2*dy); +}; - this.clone = function() { - return new Rectangle(this.x, this.y, this.width, this.height); - }; +Rectangle.prototype.inflatePoint = function(point) { + this.inflate(point.x, point.y); +}; + +Rectangle.prototype.inclusiveRangeContains = function(value, min, max) { + var isInside = + (value>=min)&& + (value<=max); - this.contains = function(x, y) { - var isInside = - (x>=this.x)&& - (y>=this.y)&& - (x=this.x)&& - (rect.y>=this.y)&& - (rect.right()<=this.right())&& - (rect.bottom()<=this.bottom()); - return isInside; - }; + if (!this.inclusiveRangeContains(minMax, aMin, aMax)|| + !this.inclusiveRangeContains(minMax, bMin, bMax)) + return null; + + return { min: maxMin, max: minMax }; +}; + +Rectangle.prototype.intersection = function(toIntersect) { + var xSpan = this.intersectRange( + this.x, this.right(), + toIntersect.x, toIntersect.right()); - this.equals = function(toCompare) { - var isIdentical = - (toCompare.x===this.x)&& - (toCompare.y===this.y)&& - (toCompare.width===this.width)&& - (toCompare.height===this.height); - return isIdentical; - }; + if (!xSpan) + return null; - this.inflate = function(dx, dy) { - this.x -= dx; - this.y -= dy; - this.width += (2*dx); - this.height += (2*dy); - }; + var ySpan = this.intersectRange( + this.y, this.bottom(), + toIntersect.y, toIntersect.bottom()); - this.inflatePoint = function(point) { - this.inflate(point.x, point.y); - }; + if (!ySpan) + return null; - this.inclusiveRangeContains = function(value, min, max) { - var isInside = - (value>=min)&& - (value<=max); - - return isInside; - }; + var result = new Rectangle( + xSpan.min, + ySpan.min, + (xSpan.max-xSpan.min), + (ySpan.max-ySpan.min)); - this.intersectRange = function(aMin, aMax, bMin, bMax) { + return result; +}; - var maxMin = Math.max(aMin, bMin); - if (!this.inclusiveRangeContains(maxMin, aMin, aMax)|| - !this.inclusiveRangeContains(maxMin, bMin, bMax)) - return null; - - var minMax = Math.min(aMax, bMax); - - if (!this.inclusiveRangeContains(minMax, aMin, aMax)|| - !this.inclusiveRangeContains(minMax, bMin, bMax)) - return null; +Rectangle.prototype.intersects = function(toIntersect) { + var intersection = this.intersection(toIntersect); - return { min: maxMin, max: minMax }; - }; - - this.intersection = function(toIntersect) { - var xSpan = this.intersectRange( - this.x, this.right(), - toIntersect.x, toIntersect.right()); - - if (!xSpan) - return null; - - var ySpan = this.intersectRange( - this.y, this.bottom(), - toIntersect.y, toIntersect.bottom()); - - if (!ySpan) - return null; - - var result = new Rectangle( - xSpan.min, - ySpan.min, - (xSpan.max-xSpan.min), - (ySpan.max-ySpan.min)); - - return result; - }; + return (typeof intersection !== 'undefined'); +}; + +Rectangle.prototype.isEmpty = function() { + return ((this.width<=0)||(this.height<=0)); +}; + +Rectangle.prototype.offset = function(dx, dy) { + this.x += dx; + this.y += dy; +}; + +Rectangle.prototype.offsetPoint = function(point) { + this.offset(point.x, point.y); +}; + +Rectangle.prototype.setEmpty = function() { + this.x = 0; + this.y = 0; + this.width = 0; + this.height = 0; +}; + +Rectangle.prototype.toString = function() { + var result = '{'; + result += '"x":'+this.x+','; + result += '"y":'+this.y+','; + result += '"width":'+this.width+','; + result += '"height":'+this.height+'}'; - this.intersects = function(toIntersect) { - var intersection = this.intersection(toIntersect); - - return (typeof intersection !== 'undefined'); - }; + return result; +}; + +Rectangle.prototype.union = function(toUnion) { + var minX = Math.min(toUnion.x, this.x); + var maxX = Math.max(toUnion.right(), this.right()); + var minY = Math.min(toUnion.y, this.y); + var maxY = Math.max(toUnion.bottom(), this.bottom()); + + var result = new Rectangle( + minX, + minY, + (maxX-minX), + (maxY-minY)); - this.isEmpty = function() { - return ((this.width<=0)||(this.height<=0)); - }; + return result; +}; + +function BucketGrid(boundingBox, rows, columns) +{ + this._boundingBox = boundingBox; + this._rows = rows; + this._columns = columns; - this.offset = function(dx, dy) { - this.x += dx; - this.y += dy; - }; + this._grid = []; - this.offsetPoint = function(point) { - this.offset(point.x, point.y); - }; + this._originLeft = boundingBox.left(); + this._originTop = boundingBox.top(); - this.setEmpty = function() { - this.x = 0; - this.y = 0; - this.width = 0; - this.height = 0; - }; + this._columnWidth = this._boundingBox.width/this._columns; + this._rowHeight = this._boundingBox.height/this._rows; - this.toString = function() { - var result = '{'; - result += '"x":'+this.x+','; - result += '"y":'+this.y+','; - result += '"width":'+this.width+','; - result += '"height":'+this.height+'}'; + for (var rowIndex = 0; rowIndex