Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Closure v Prototypical Objects
(version: 0)
Comparing performance of:
Instantiation - Closure vs Instantiation - Prototype vs Utilization - Closure vs Utilization - Prototype
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function Foo() { 'use strict'; let self = {}; /**************************************************************************** * Properties ***************************************************************************/ self.foo1 = 15; self.foo2 = 'testing'; self.foo3 = { i: 1, am: 2, a: 3, literal: 4 }; self.foo4 = null; /**************************************************************************** * Methods ***************************************************************************/ self.constructor = function () { self.foo4 = self.foo2 + self.foo1; } self.doFoo1 = function () { return self.foo2; } self.doFoo2 = function () { self.foo1 += 1; return self.foo1; } /**************************************************************************** * Public Exports ***************************************************************************/ self.public = { doFoo1: self.doFoo1, doFoo2: self.doFoo2 } self.constructor(); return self.public; } preFoo = Foo(); function Bar() { this._foo1 = 15; this._foo2 = 'testing'; this._foo3 = { i: 1, am: 2, a: 3, literal: 4 }; this._foo4 = null; } Bar.prototype.doFoo1 = function () { return this._foo2; } Bar.prototype.doFoo2 = function () { this._foo1 += 1; return this._foo1; } preBar = new Bar();
Tests:
Instantiation - Closure
let foo = Foo();
Instantiation - Prototype
let bar = new Bar();
Utilization - Closure
let baz = preFoo.doFoo2();
Utilization - Prototype
let baz = preBar.doFoo2();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Instantiation - Closure
Instantiation - Prototype
Utilization - Closure
Utilization - Prototype
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 break down the provided benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Definition** The benchmark measures the performance of JavaScript closures vs. prototypical objects in two scenarios: 1. **Instantiation**: Creating instances of classes `Foo` and `Bar`. 2. **Utilization**: Calling methods on instances created with different approaches (closure or prototype). **Script Preparation Code** The script defines two classes, `Foo` and `Bar`, with the following characteristics: * `Foo` has properties (`foo1`, `foo2`, `foo3`) and methods (`constructor`, `doFoo1`, `doFoo2`). The constructor assigns a value to `foo4`. * `Bar` has private properties (`_foo1`, `_foo2`, `_foo3`) and methods (`doFoo1`, `doFoo2`) that access these properties. * Both classes have a public export (`public`) containing the method references. **Options Compared** The benchmark compares two approaches: 1. **Closure**: Creating instances of `Foo` using `new Foo()` or `preFoo.doFoo2()`. This creates a closure, where the instance's scope is tied to its creation. 2. **Prototype**: Creating instances of `Bar` using `new Bar()` or `preBar.doFoo2()`. This uses prototypical inheritance, where the instance inherits properties and methods from its prototype. **Pros and Cons** * **Closure** + Pros: - Encapsulation: data is tightly bound to the instance's scope. - Simplified memory management. + Cons: - Less flexible than prototypal inheritance. - May lead to memory leaks if not properly cleaned up. * **Prototype** + Pros: - More flexible and extensible than closure-based approach. - Easier to manage memory, as instances inherit from the prototype. + Cons: - Less encapsulation: data is accessible from outside the instance. - May lead to tighter coupling between classes. **Other Considerations** * **Instance creation**: Creating instances using `new` versus using method references (`preFoo.doFoo2()`) can affect performance, as the latter may involve function calls and lookups. * **Method access**: The benchmark measures the execution speed of accessing methods on instances. This can reveal differences in how closures and prototypes handle method invocation. **Alternative Approaches** Other approaches to consider: 1. **Function-based programming**: Instead of using classes, use functions as self-contained units. This could simplify memory management and reduce overhead. 2. **Module-based systems**: Use modules or ES6 imports to encapsulate code and manage dependencies. This approach may provide better performance and modularity benefits. Please note that this analysis is based on the provided benchmark definition and script preparation code. The actual results might depend on various factors, including the specific JavaScript engine, hardware, and version.
Related benchmarks:
Lodash isEqual test with different number of properties
Closure v Prototypical Objects 2
Raw object with Closure vs Prototypical Objects
Measuring impact of closures
Comments
Confirm delete:
Do you really want to delete benchmark?