Editing Low Level Optimizations This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) This jsperf uses optimizations that a compiler can do with compiled languages like C/C++. When working with animations (canvas, webgl), we need to calculate a lot of positions, triangles and things like this. This types of calculations occur many times per second, so would be great to see performance improvements in this area. Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var x = 10 , y = 0 , angle = Math.PI/180 , testLen = 360 , tx = 0 , ty = 0; while (testLen--) { tx = x; ty = y; x = tx * Math.cos(angle) - ty * Math.sin(angle); y = tx * Math.sin(angle) + ty * Math.cos(angle); } Test 2 Title Async (check if this is an asynchronous test) Code var x = 10 , y = 0 , angle = Math.PI/180 , testLen = 360; function rotateX(x, angle) { return x * Math.cos(angle) - y * Math.sin(angle); } function rotateY(y, angle) { return x * Math.sin(angle) + y * Math.cos(angle); } while (testLen--) { x = rotateX(x, angle); y = rotateY(y, angle); } Test 3 Title Async (check if this is an asynchronous test) Code var x = 10 , y = 0 , angle = Math.PI/180 , testLen = 360 , tx = 0 , ty = 0; function rotate() { tx = x; ty = y; x = tx * Math.cos(angle) - ty * Math.sin(angle); y = tx * Math.sin(angle) + ty * Math.cos(angle); } while (testLen--) { rotate(); } Test 4 Title Async (check if this is an asynchronous test) Code var o = {x: 10, y: 0} , angle = Math.PI/180 , testLen = 360 , tx = 0 , ty = 0; function rotate(o, angle) { tx = o.x; ty = o.y; o.x = tx * Math.cos(angle) - ty * Math.sin(angle); o.x = tx * Math.sin(angle) + ty * Math.cos(angle); } while (testLen--) { rotate(o, angle); } Test 5 Title Async (check if this is an asynchronous test) Code var angle = Math.PI/180 , testLen = 360; function Obj() { this.x = 10; this.y = 0; this.rotate = function(angle) { var tx = this.x, ty = this.y; this.x = tx * Math.cos(angle) - ty * Math.sin(angle); this.x = tx * Math.sin(angle) + ty * Math.cos(angle); } } var o = new Obj(); while (testLen--) { o.rotate(angle); } Test 6 Title Async (check if this is an asynchronous test) Code var angle = Math.PI/180 , testLen = 360; function Obj() { this.x = 10; this.y = 0; } Obj.prototype.rotate = function(angle) { var tx = this.x, ty = this.y; this.x = tx * Math.cos(angle) - ty * Math.sin(angle); this.x = tx * Math.sin(angle) + ty * Math.cos(angle); } var o = new Obj(); while (testLen--) { o.rotate(angle); } Test 7 Title Async (check if this is an asynchronous test) Code var array = new Float64Array([10, 0]) , angle = Math.PI/180 , testLen = 360; while (testLen--) { var tx = array[0], ty = array[1]; array[0] = tx * Math.cos(angle) - ty * Math.sin(angle); array[1] = tx * Math.sin(angle) + ty * Math.cos(angle); }