Editing Extend function comparisons 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) Comparing jQuery and Underscore extend() functions for performance. 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 src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="http://documentcloud.github.com/underscore/underscore-min.js"></script> <script src="//sjhcockrell.s3.amazonaws.com/downloads/belt.js"></script> <script src="//sjhcockrell.s3.amazonaws.com/downloads/belt.min.js"></script> <script> var Bmin = B.noConflict(); </script> <script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.3/lodash.min.js"></script> <script> var lodash = _.noConflict(); </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) var targetObject, extendingObject, targetArray = [], extendingArray = []; // Populate Arrays. for (var i = 0; i < 10000; i += 1) { targetArray[i] = i; extendingArray[i] = (i * 12) / 3; } targetObject = { a: [], b: { one: "one", two: { point1: 2.1, point2: 2.2, point3: 2.3 }, three: [3.1, 3.2, 3.3, 3.4] }, c: function(b) { b = 1; return b }, d: "okay", e: 1 }; extendingObject = { prop: function(){ return "prop"; }, d: targetArray, f: function getE() { this.e = this.e * 1000; return this.e; } }; var objectTypes = { 'boolean': false, 'function': true, 'object': true, 'number': false, 'string': false, 'undefined': false }; var objectTypes = { 'boolean': false, 'function': true, 'object': true, 'number': false, 'string': false, 'undefined': false }; var nativeKeys = Object.keys; var X = { extend: function(object, source, guard) { var index, iteratee = object, result = object; if (!object) return result; var argsIndex = 1, argsLength = typeof guard == 'number' ? 2 : arguments.length; for (;argsIndex < argsLength; argsIndex++) { if ((iteratee = arguments[argsIndex])) { var skipProto = typeof iteratee == 'function' && propertyIsEnumerable.call(iteratee, 'prototype'); var ownIndex = -1, ownProps = objectTypes[typeof iteratee] ? nativeKeys(iteratee) : [], length = ownProps.length; while (++ownIndex < length) { index = ownProps[ownIndex]; if (!(skipProto && index == 'prototype')) { result[index] = iteratee[index] } } } } return result; }, extend_o: function (object, source, guard) { var index, iteratee = object, result = object; if (!object) return result; for (var argsIndex = 1, argsLength = typeof guard == 'number' ? 2 : arguments.length; argsIndex < argsLength; argsIndex++) { if ((iteratee = arguments[argsIndex])) {; var skipProto = typeof iteratee == 'function' && propertyIsEnumerable.call(iteratee, 'prototype'); var ownIndex = -1, ownProps = objectTypes[typeof iteratee] ? nativeKeys(iteratee) : [], length = ownProps.length; while (++ownIndex < length) { index = ownProps[ownIndex]; if (!(skipProto && index == 'prototype')) { result[index] = iteratee[index] } } } }; return result } }; 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 X.extend_o(targetObject, extendingObject); Test 2 Title Async (check if this is an asynchronous test) Code B.extend(targetObject, extendingObject); Test 3 Title Async (check if this is an asynchronous test) Code lodash.extend(targetObject, extendingObject); Test 4 Title Async (check if this is an asynchronous test) Code // Cleaned up version of lodash. X.extend(targetObject, extendingObject); Test 5 Title Async (check if this is an asynchronous test) Code B.extend_if(targetObject, extendingObject);