Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object Composition vs Instantiation
(version: 0)
Comparing performance of:
Instantiation vs Composition
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ "use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var barker = function barker(state) { return { bark: function bark() { return console.log("woof, I am " + state.name); } }; }; var driver = function driver(state) { return { drive: function drive() { return state.position = state.position + state.speed; } }; }; var robotDog = function robotDog(name) { var state = { name: name, speed: 100, position: 0 }; return Object.assign({}, barker(state), driver(state)); }; var Driver = function () { function Driver() { _classCallCheck(this, Driver); } _createClass(Driver, [{ key: "drive", value: function drive() { console.log("I am driving"); } }]); return Driver; }(); var Barker = function (_Driver) { _inherits(Barker, _Driver); function Barker(name) { _classCallCheck(this, Barker); var _this = _possibleConstructorReturn(this, (Barker.__proto__ || Object.getPrototypeOf(Barker)).call(this)); _this.name = name; return _this; } _createClass(Barker, [{ key: "bark", value: function bark() { console.log("woof, I am " + this.name); } }]); return Barker; }(Driver); window.robotDog = robotDog; window.Barker = Barker; },{}]},{},[1])
Tests:
Instantiation
var red = new Barker("red");
Composition
var yellow = robotDog("yellow");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Instantiation
Composition
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll do my best to explain what's being tested in this benchmark. The provided JSON represents a JavaScript microbenchmarking test, specifically testing the performance difference between two approaches: object composition and instantiation. **Benchmark Definition** The test consists of two individual test cases: 1. **Instantiation**: This test case creates an instance of the `Barker` class using the `new` keyword: `var red = new Barker("red");`. The benchmark is measuring how fast this code executes. 2. **Composition**: This test case creates an instance of the `robotDog` function, which returns an object with two properties: `bark` and `drive`: `var yellow = robotDog("yellow");`. The benchmark is measuring how fast this code executes. **Benchmark Code** The provided JavaScript code defines several functions: * `barker(state)`: Returns an object with a `bark` method that logs "woof, I am <state.name>" to the console. * `driver(state)`: Returns an object with a `drive` method that updates `state.position` by adding `state.speed`. * `robotDog(name)`: Returns an object with `bark` and `drive` methods after merging the results of `barker` and `driver` using `Object.assign`. **Options Compared** The two test cases compare two approaches: 1. **Instantiation**: Creates a new instance of the `Barker` class. 2. **Composition**: Creates an instance of the `robotDog` function, which returns an object. **Pros and Cons** * **Instantiation**: Pros: * Easier to understand and maintain. * More explicit creation of objects. Cons: * May incur overhead due to dynamic property lookup and prototype chain traversal. * **Composition**: Pros: * Can lead to more efficient code, as properties are directly attached to the object's prototype. Cons: * Requires a deeper understanding of JavaScript object models. **Library** The `Object.assign()` method is used in the benchmark code. This method is a part of the ECMAScript standard and allows copying values from an existing object into a new object. **Special JS Features or Syntax** This benchmark uses modern JavaScript features, such as: * **Arrow functions**: Used for creating small, anonymous functions. * **Template literals**: Used for string interpolation. * **Spread syntax**: Used to merge objects and arrays. These features are widely supported in modern browsers and can improve code readability and maintainability. **Alternatives** Other alternatives for composition include: * Using constructors like `Barker` to create instances. * Utilizing classes and the `this` keyword to define properties and methods. However, the composition approach used in this benchmark is a common pattern in JavaScript programming.
Related benchmarks:
const vs let vs var
var vs let vs const
Object creation vs function definition vs arror function vs function expression vs named function expression
const vs let vs var fixed
const vs let vs var fork
Comments
Confirm delete:
Do you really want to delete benchmark?