Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Prototype vs simulated private bench
(version: 0)
Comparing performance of:
Simulated private vs Prototype vs Prototype (single call)
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var r = r || 100;
Tests:
Simulated private
( function (r, resultsA) { (function (root, factory) { var moduleName = 'TestA'; if (typeof define === 'function' && define.amd) { define(moduleName, factory); } else if (typeof exports === 'object') { module.exports = factory(); } else { root[moduleName] = factory(moduleName); } }(this, function (moduleName) { function TestA(element) { this.element = element; var self = this; var a = function () { return "a" }; var b = function () { return "b" }; var c = function () { return "c" }; (function initA() { console.log(self.element, a(), b(), c()); }()); } return TestA })); var r = r || 100; var resultsA = []; for(var i = 0; i < r; i++){ var d = new Date().getTime(); resultsA.push(new TestA(d)); } }());
Prototype
( function (r, resultsB) { (function (root, factory) { var moduleName = 'TestB'; if (typeof define === 'function' && define.amd) { define(moduleName, factory); } else if (typeof exports === 'object') { module.exports = factory(); } else { root[moduleName] = factory(moduleName); } }(this, function (moduleName) { function TestB(element) { this.element = element; this.initB(); } TestB.prototype.initB = function () { console.log(this.element, this.a(), this.b(), this.c()); }; TestB.prototype.a = function () { return "a" }; TestB.prototype.b = function () { return "b" }; TestB.prototype.c = function () { return "c" }; return TestB })); var r = r || 100; var resultsB = []; for(var i = 0; i < r; i++){ var d = new Date().getTime(); resultsB.push(new TestB(d)); } }());
Prototype (single call)
( function (r, resultsC) { (function (root, factory) { var moduleName = 'TestC'; if (typeof define === 'function' && define.amd) { define(moduleName, factory); } else if (typeof exports === 'object') { module.exports = factory(); } else { root[moduleName] = factory(moduleName); } }(this, function (moduleName) { function TestC(element) { this.element = element; this.initC(); } TestC.prototype = { constructor: TestC, initC: function() { console.log(this.element, this.a(), this.b(), this.c()); }, a: function() { return "a" }, b: function() { return "b" }, c: function() { return "c" } } return TestC })); var r = r || 100; var resultsC = []; for(var i = 0; i < r; i++){ var d = new Date().getTime(); resultsC.push(new TestC(d)); } }());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Simulated private
Prototype
Prototype (single call)
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'm ready to break down the benchmark and explain what's being tested, compared, and their pros/cons. **Benchmark Overview** The benchmark measures the performance difference between three approaches: Prototype, Simulated Private, and Prototype (single call). The test creates an instance of a class for each iteration, which performs some simple calculations. The goal is to compare the execution speed of these different approaches in JavaScript. **Prototype Approach** In the Prototype approach, objects are created using the `new` keyword, and their properties are dynamically added or modified. In this case, the `TestA` class has several methods (`a`, `b`, and `c`) that are defined on the prototype chain of each instance. The `initA` method is called to perform some calculations. Pros: * Easy to implement and understand * Allows for dynamic property addition or modification Cons: * Can lead to slower performance due to property lookup and binding * May cause issues with caching and optimization **Simulated Private Approach** In the Simulated Private approach, a private class is created using an Immediately Invoked Function Expression (IIFE) and the `var` keyword. The `TestB` class has methods (`a`, `b`, and `c`) defined on its prototype chain. Pros: * Can provide better encapsulation and data hiding * May lead to faster performance due to reduced property lookup Cons: * Requires a private syntax, which may not be supported by all browsers or environments * Can make the code harder to understand and maintain **Prototype (Single Call) Approach** In the Prototype (single call) approach, an object is created with a single constructor function. This constructor function has methods (`a`, `b`, and `c`) defined inline. Pros: * May lead to faster performance due to reduced property lookup * Can be more efficient for simple calculations Cons: * Limits flexibility in defining properties or behaviors * May not be suitable for complex objects with multiple inheritance **Comparison** The benchmark tests the execution speed of each approach on a desktop Linux system with Firefox 62. The results show that: 1. Prototype (single call) is the fastest, followed closely by Simulated Private. 2. Prototype has slower performance compared to the other two approaches. It's essential to note that these results may not be representative of all scenarios or environments. The choice of approach depends on the specific requirements and constraints of your project. In conclusion, while each approach has its strengths and weaknesses, Prototype (single call) appears to be the fastest in this benchmark. However, Simulated Private offers a good balance between performance and encapsulation, making it a suitable alternative for more complex use cases.
Related benchmarks:
Data Properties vs. Accessor Properties vs. Getter / Setter Methods
soa vs aos
Data Properties vs. Accessor Properties vs. Getter / Setter Methods 1
Data Properties vs. Accessor Properties vs. Getter / Setter Methods 2
ES6 property (get/set) & getter/setter function vs direct access to object attribute (private and not) v2
Comments
Confirm delete:
Do you really want to delete benchmark?