(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])
var red = new Barker("red");
var yellow = robotDog("yellow");
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Instantiation | |
Composition |
Test name | Executions per second |
---|---|
Instantiation | 7235619.0 Ops/sec |
Composition | 2888534.0 Ops/sec |
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:
Barker
class using the new
keyword: var red = new Barker("red");
. The benchmark is measuring how fast this code executes.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:
Barker
class.robotDog
function, which returns an object.Pros and Cons
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:
These features are widely supported in modern browsers and can improve code readability and maintainability.
Alternatives
Other alternatives for composition include:
Barker
to create instances.this
keyword to define properties and methods.However, the composition approach used in this benchmark is a common pattern in JavaScript programming.