diff --git a/src/core/graphics/Graphics.js b/src/core/graphics/Graphics.js index f341e56..520d077 100644 --- a/src/core/graphics/Graphics.js +++ b/src/core/graphics/Graphics.js @@ -637,12 +637,22 @@ */ Graphics.prototype.drawPolygon = function (path) { - if (!(path instanceof Array)) + // prevents an argument assignment deopt + // see section 3.1: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments + var points = path; + + if (!Array.isArray(points)) { - path = Array.prototype.slice.call(arguments); + // prevents an argument leak deopt + // see section 3.2: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments + points = new Array(arguments.length); + + for (var i = 0; i < points.length; ++i) { + points[i] = arguments[i]; + } } - this.drawShape(new math.Polygon(path)); + this.drawShape(new math.Polygon(points)); return this; }; diff --git a/src/core/graphics/Graphics.js b/src/core/graphics/Graphics.js index f341e56..520d077 100644 --- a/src/core/graphics/Graphics.js +++ b/src/core/graphics/Graphics.js @@ -637,12 +637,22 @@ */ Graphics.prototype.drawPolygon = function (path) { - if (!(path instanceof Array)) + // prevents an argument assignment deopt + // see section 3.1: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments + var points = path; + + if (!Array.isArray(points)) { - path = Array.prototype.slice.call(arguments); + // prevents an argument leak deopt + // see section 3.2: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments + points = new Array(arguments.length); + + for (var i = 0; i < points.length; ++i) { + points[i] = arguments[i]; + } } - this.drawShape(new math.Polygon(path)); + this.drawShape(new math.Polygon(points)); return this; }; diff --git a/src/core/math/shapes/Polygon.js b/src/core/math/shapes/Polygon.js index d5a89f1..636456b 100644 --- a/src/core/math/shapes/Polygon.js +++ b/src/core/math/shapes/Polygon.js @@ -10,15 +10,25 @@ * arguments passed can be flat x,y values e.g. `new Polygon(x,y, x,y, x,y, ...)` where `x` and `y` are * Numbers. */ -function Polygon(points) +function Polygon(points_) { + // prevents an argument assignment deopt + // see section 3.1: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments + var points = points_; + //if points isn't an array, use arguments as the array - if (!(points instanceof Array)) + if (!Array.isArray(points)) { - points = Array.prototype.slice.call(arguments); + // prevents an argument leak deopt + // see section 3.2: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments + points = new Array(arguments.length); + + for (var i = 0; i < points.length; ++i) { + points[i] = arguments[i]; + } } - //if this is an array of points, convert it to a flat array of numbers + // if this is an array of points, convert it to a flat array of numbers if (points[0] instanceof Point) { var p = [];