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) - prevent prototype changes being shared between tests - add a version that precompiles a cached mixin for the "old-style" objects 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 = Array.prototype.slice.call(arguments, 0); return function() { return fn.apply(this, args.concat(Array.prototype.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; }; } //6 var makeCopier = function (obj) { var code = ''; for (var k in obj) { if (Object.prototype.hasOwnProperty.call (obj, k)) { code += 'this.' + k + ' = obj.' + k + '; '; }; }; return new Function ('obj', 'return function () { ' + code + ' };')(obj); }, circleFnsInit = makeCopier(circleFns); //set up test constructor var origCircularObjectPrototype = function() { return {}; }, 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 CircularObject.prototype = origCircularObjectPrototype(); 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 CircularObject.prototype = origCircularObjectPrototype(); asCircle.call(CircularObject.prototype); var obj = new CircularObject(4); obj.shrink(); Test 3 Title Async (check if this is an asynchronous test) Code CircularObject.prototype = origCircularObjectPrototype(); asCircleCached.call(CircularObject.prototype); var obj = new CircularObject(4); obj.shrink(); Test 4 Title Async (check if this is an asynchronous test) Code CircularObject.prototype = origCircularObjectPrototype(); 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 CircularObject.prototype = origCircularObjectPrototype(); 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 CircularObject.prototype = origCircularObjectPrototype(); circleFnsInit.call (CircularObject); var obj = new CircularObject(4); obj.shrink;