Editing The Power Of Getters - All Together 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) 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 defineLazyAccessor = function() { var O = Object, defineProperty = O.defineProperty, dProto = O.create(null), dThis = O.create(null) ; dThis.configurable = true; return function defineLazyAccessor( proto, name, getNewValue, notEnumerable ) { dProto.enumerable = !notEnumerable; dProto.get = function () { dThis.enumerable = !notEnumerable; dThis.value = getNewValue.call(this); return defineProperty(this, name, dThis)[name]; }; defineProperty(proto, name, dProto); }; }(); function defineLazyAccessors(proto, descriptors) { for (var key, curr, length, keys = Object.keys(descriptors), i = 0; i < keys.length; ) { curr = descriptors[ key = keys[i++] ]; defineLazyAccessor( proto, key, curr.get, !curr.enumerable ); if (curr.preserve) keys.splice(--i, 1); } length = keys.length; return function cleanUp(self) { self || (self = this); for(i = 0; i < length; delete self[keys[i++]]); return self; } } function firstChild() { return this.children[0]; } function lastChild() { return this.children[ this.children.length - 1 ]; } function Element(){} var cleanUp = defineLazyAccessors( Element.prototype, { children: { preserve: true, enumerable: true, get: function () { return []; } }, firstChild: { get: firstChild }, lastChild: { get: lastChild } }); Element.prototype.appendChild = function (el) { cleanUp(this).children.push(el); return el; }; function Element2() { this.children = []; } Element2.prototype.appendChild = function (el) { this.children.push(el); return el; }; Object.defineProperties( Element2.prototype, { firstChild: { get: firstChild }, lastChild: { get: lastChild } }); function benchIt(Element) { // 200 instances for (var i = 0; i < 200; i++) { var el = new Element; // 5 appendChild of new Element per instance for (var j = 0; j < 5; j++) { el.appendChild(new Element); } // 100 firstChild and lastChild access for (j = 0; j < 100; j++) { result = el.firstChild && el.lastChild; } } } function Element3() {} Element3.prototype.getFirstChild = function () { return this.getChildren()[0]; }; Element3.prototype.getLastChild = function () { var children = this.getChildren(); return children[children.length - 1]; }; Element3.prototype.appendChild = function (el) { this.getChildren().push(el); return el; }; // simulating lazy property assignment Element3.prototype.getChildren = function () { var children = []; return (this.getChildren = function () { return children; })(); }; var 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 benchIt(Element); Test 2 Title Async (check if this is an asynchronous test) Code benchIt(Element2); Test 3 Title Async (check if this is an asynchronous test) Code for (var i = 0; i < 200; i++) { var el = new Element3; // 5 appendChild of new Element3 for (var j = 0; j < 5; j++) { el.appendChild(new Element3); } // 100 firstChild and lastChild access for (j = 0; j < 100; j++) { result = el.getFirstChild() && el.getLastChild(); } }