Editing mixin fun 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) 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) <script> //1 var circleFns = { area: function() { return Math.PI * this.radius * this.radius; }, grow: function() { this.radius++; }, shrink: function() { this.radius--; } } //2 var asCircle = function() { this.area = function() { return Math.PI * this.radius * this.radius; }; this.grow = function() { this.radius++; }; this.shrink = function() { this.radius--; }; } //3 var asCircleCached = (function() { var area = function() { return Math.PI * this.radius * this.radius; }; var grow = function() { this.radius++; }; var shrink = function() { this.radius--; }; return function() { this.area = area, this.grow = grow, this.shrink = shrink; } })(); //4 Function.prototype.curry = function() { var fn = this; var args = [].slice.call(arguments, 0); return function() { return fn.apply(this, args.concat([].slice.call(arguments, 0))); } } //4 var asCircleCachedAndCurried = (function() { var area = function() { return Math.PI * this.radius * this.radius; }; var grow = function(growBy) { this.radius += growBy; }; var shrink = function(shrinkBy) { this.radius -= shrinkBy; }; return function(options) { this.area = area, this.grow = grow.curry(options['growBy']), this.shrink = shrink.curry(options['shrinkBy']) } })(); //5 var asCircleWithOptions = function(options) { this.area = function() { return Math.PI * this.radius * this.radius; }; this.grow = function() { this.radius += options.growBy; }; this.shrink = function() { this.radius -= options.shrinkBy; }; } // 7 var asCircleCached2 = (function() { var area = function() { return Math.PI * this.radius * this.radius; }; var grow = function() { this.radius++; }; var shrink = function() { this.radius--; }; return { area: area, grow: grow, shrink: shrink } })(); //set up test constructor var CircularObject = function(radius) { this.radius = radius }; </script> 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 for (var k in circleFns) { if (circleFns.hasOwnProperty(k)) { CircularObject.prototype[k] = circleFns[k] }; } var obj = new CircularObject(4); obj.shrink(); Test 2 Title Async (check if this is an asynchronous test) Code asCircle.call(CircularObject.prototype); var obj = new CircularObject(4); obj.shrink(); Test 3 Title Async (check if this is an asynchronous test) Code asCircleCached.call(CircularObject.prototype); var obj = new CircularObject(4); obj.shrink(); Test 4 Title Async (check if this is an asynchronous test) Code asCircleCachedAndCurried.call( CircularObject.prototype, { growBy: 2, shrinkBy: 2 }); var obj = new CircularObject(4); obj.shrink(); Test 5 Title Async (check if this is an asynchronous test) Code asCircleWithOptions.call( CircularObject.prototype, { growBy: 2, shrinkBy: 2 }); var obj = new CircularObject(4); obj.shrink(); Test 6 Title Async (check if this is an asynchronous test) Code for (var k in circleFns) { CircularObject.prototype[k] = circleFns[k] } var obj = new CircularObject(4); obj.shrink(); Test 7 Title Async (check if this is an asynchronous test) Code asCircleCached2.call(CircularObject.prototype); var obj = new CircularObject(4); obj.shrink();